# Spring Cloud-Consul
3.1.0
该项目通过自动配置和绑定到 Spring 环境和其他 Spring 编程模型习惯用法,为 Spring 引导应用程序提供 Consul 集成。通过一些简单的注释,你可以快速启用和配置应用程序内的公共模式,并使用基于 Consul 的组件构建大型分布式系统。所提供的模式包括服务发现、控制总线和配置。智能路由和客户端负载平衡、断路器是通过与其他 Spring Cloud项目集成来提供的。
# 1.快速启动
这一快速启动将使用 Spring Cloud Consul 进行服务发现和分布式配置。
首先,在你的机器上运行领事代理。然后,你可以访问它,并将其作为服务注册中心和配置源使用 Spring Cloud Consul。
# 1.1.发现客户端使用情况
要在应用程序中使用这些特性,你可以将其构建为依赖于spring-cloud-consul-core
的 Spring 引导应用程序。添加依赖项最方便的方法是使用 Spring 引导启动器:org.springframework.cloud:spring-cloud-starter-consul-discovery
。我们建议使用依赖管理和spring-boot-starter-parent
。下面的示例显示了典型的 Maven 配置:
POM.xml
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-version}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
下面的示例显示了一个典型的 Gradle 设置:
建造。 Gradle
plugins {
id 'org.springframework.boot' version ${spring-boot-version}
id 'io.spring.dependency-management' version ${spring-dependency-management-version}
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
现在,你可以创建一个标准的 Spring 启动应用程序,例如下面的 HTTP 服务器:
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当这个 HTTP 服务器运行时,它会连接到运行在默认的本地 8500 端口的 Consul 代理。要修改启动行为,可以使用应用程序.属性
更改 Consul 代理的位置,如下例所示:
spring:
cloud:
consul:
host: localhost
port: 8500
你现在可以使用DiscoveryClient
、@LoadBalanced RestTemplate
或@LoadBalanced WebClient.Builder
从 Consul 检索服务和实例数据,如以下示例所示:
@Autowired
private DiscoveryClient discoveryClient;
public String serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri().toString();
}
return null;
}
# 1.2.分布式配置使用
要在应用程序中使用这些特性,你可以将其构建为依赖于spring-cloud-consul-core
和spring-cloud-consul-config
的 Spring 引导应用程序。添加依赖项最方便的方法是使用 Spring 引导启动器:org.springframework.cloud:spring-cloud-starter-consul-config
。我们建议使用依赖管理和spring-boot-starter-parent
。下面的示例显示了典型的 Maven 配置:
POM.xml
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{spring-boot-version}</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
下面的示例显示了一个典型的 Gradle 设置:
构建。 Gradle
plugins {
id 'org.springframework.boot' version ${spring-boot-version}
id 'io.spring.dependency-management' version ${spring-dependency-management-version}
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-consul-config'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
现在,你可以创建一个标准的 Spring 启动应用程序,例如下面的 HTTP 服务器:
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
应用程序从 Consul 检索配置数据。
如果使用 Spring Cloud Consul Config,则需要设置spring.config.import 属性才能绑定到 Consul。你可以在Spring Boot Config Data Import section中阅读有关它的更多信息。 |
---|
# 2.安装领事
有关如何安装 Consul 的说明,请参见安装文档 (opens new window)。
# 3.领事代理人
Consul 代理客户端必须可用于所有 Spring Cloud Consul 应用程序。默认情况下,代理客户机应该位于localhost:8500
。有关如何启动代理客户机以及如何连接到 Consul 代理服务器集群的详细信息,请参见代理文档 (opens new window)。为了进行开发,在安装了 Consul 之后,可以使用以下命令启动 Consul 代理:
./src/main/bash/local_run_consul.sh
这将在端口 8500 上以服务器模式启动一个代理,其 UI 位于本地主机:8500 (opens new window)。
# 4.与领事的服务发现
服务发现是基于微服务的体系结构的关键原则之一。尝试手动配置每个客户机或某种形式的约定可能非常困难,并且可能非常脆弱。领事通过HTTP API (opens new window)和DNS (opens new window)提供服务发现服务。 Spring Cloud Consul 利用 HTTP API 进行服务注册和发现。这并不妨碍非 Spring Cloud应用程序利用 DNS 接口。Consul 代理服务器在cluster (opens new window)中运行,该服务器通过八卦协议 (opens new window)进行通信,并使用RAFT 共识协议 (opens new window)。
# 4.1.如何激活
要激活 Consul 服务发现,请使用分组org.springframework.cloud
和工件 IDspring-cloud-starter-consul-discovery
的启动器。请参阅Spring Cloud Project page (opens new window)以获取有关使用当前 Spring Cloud发布列设置构建系统的详细信息。
# 4.2.向领事登记
当客户机向 Consul 注册时,它提供有关自身的元数据,如主机和端口、ID、名称和标记。默认情况下,创建一个 HTTP,Consul 每 10 秒钟点击端点。如果健康检查失败,服务实例将被标记为“关键”。
示例领事客户:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
(即完全正常的引导应用程序)。如果 Consul 客户机位于localhost:8500
以外的地方,则需要配置来定位客户机。示例:
应用程序.yml
spring:
cloud:
consul:
host: localhost
port: 8500
如果你使用Spring Cloud Consul Config,并且你已经设置了spring.cloud.bootstrap.enabled=true 或spring.config.use-legacy-processing=true 或使用spring-cloud-starter-bootstrap ,那么上述值将需要放置在bootstrap.yml 而不是应用程序.yml 中。 |
---|
默认的服务名、实例 ID 和端口(取自Environment
)分别是${spring.application.name}
、 Spring 上下文 ID 和${server.port}
。
要禁用 Consul Discovery 客户端,你可以将spring.cloud.consul.discovery.enabled
设置为false
。当spring.cloud.discovery.enabled
设置为false
时,Consul Discovery 客户端也将被禁用。
要禁用服务注册,你可以将spring.cloud.consul.discovery.register
设置为false
。
# 4.2.1.将管理注册为一项单独的服务
当管理服务器端口被设置为与应用程序端口不同的东西时,通过设置management.server.port
属性,管理服务将被注册为独立于应用程序服务的服务。例如:
应用程序.yml
spring:
application:
name: myApp
management:
server:
port: 4452
上述配置将注册以下 2 项服务:
- 应用程序服务:
ID: myApp
Name: myApp
- 管理服务:
ID: myApp-management
Name: myApp-management
管理服务将从应用程序服务继承其instanceId
和serviceName
。例如:
应用程序.yml
spring:
application:
name: myApp
management:
server:
port: 4452
spring:
cloud:
consul:
discovery:
instance-id: custom-service-id
serviceName: myprefix-${spring.application.name}
上述配置将注册以下 2 项服务:
- 应用程序服务:
ID: custom-service-id
Name: myprefix-myApp
- 管理服务:
ID: custom-service-id-management
Name: myprefix-myApp-management
可以通过以下属性进行进一步的定制:
/** Port to register the management service under (defaults to management port) */
spring.cloud.consul.discovery.management-port
/** Suffix to use when registering management service (defaults to "management" */
spring.cloud.consul.discovery.management-suffix
/** Tags to use when registering management service (defaults to "management" */
spring.cloud.consul.discovery.management-tags
# 4.2.2.HTTP 健康检查
Consul 实例的健康检查默认为“/actuator/health”,这是 Spring 引导执行器应用程序中健康端点的默认位置。如果使用非默认的上下文路径或 Servlet 路径(例如server.servletPath=/foo
)或管理端点路径(例如management.server.servlet.context-path=/admin
),则即使对于执行器应用程序,也需要对此进行更改。
Consul 用于检查健康端点的间隔也可以配置。“10 秒”和“1 米”分别代表 10 秒和 1 分钟。
此示例演示了上面的内容(有关更多选项,请参见附录页中的spring.cloud.consul.discovery.health-check-*
属性)。
应用程序.yml
spring:
cloud:
consul:
discovery:
healthCheckPath: ${management.server.servlet.context-path}/actuator/health
healthCheckInterval: 15s
你可以通过设置spring.cloud.consul.discovery.register-health-check=false
完全禁用 HTTP 健康检查。
# 应用头文件
头可以应用于健康检查请求。例如,如果你试图注册一个Spring Cloud Config (opens new window)服务器,该服务器使用保险库后端 (opens new window):
应用程序.yml
spring:
cloud:
consul:
discovery:
health-check-headers:
X-Config-Token: 6442e58b-d1ea-182e-cfa5-cf9cddef0722
根据 HTTP 标准,每个头可以有多个值,在这种情况下,可以提供一个数组:
应用程序.yml
spring:
cloud:
consul:
discovery:
health-check-headers:
X-Config-Token:
- "6442e58b-d1ea-182e-cfa5-cf9cddef0722"
- "Some other value"
# 4.2.3.执行器健康指示器
如果服务实例是 Spring 启动致动器应用程序,则可以提供以下致动器的健康指示器。
# 发现潜在的指示剂
当 Consul 服务发现是活动的时,发现客户状态指示器 (opens new window)被配置并使致动器健康端点可用。有关配置选项,请参见here (opens new window)。
# 健康咨询指示器
配置了一个指示器,用于验证ConsulClient
的健康状况。
默认情况下,它检索 Consul Leader 节点状态和所有已注册的服务。在具有许多注册服务的部署中,在每次健康检查中检索所有服务的成本可能很高。跳过服务检索,只检查 leader 节点状态集spring.cloud.consul.health-indicator.include-services-query=false
。
禁用指示器集management.health.consul.enabled=false
。
当应用程序在Bootstrap 上下文模式 (opens new window)(默认值)中运行时, 此指示器将被加载到 BootStrap 上下文中,并且不对 Actuator Health 端点可用。 |
---|
# 4.2.4.元数据
Consul 支持服务的元数据。 Spring Cloud的ServiceInstance
具有一个Map<String, String> metadata
字段,该字段由服务meta
字段填充。在spring.cloud.consul.discovery.metadata
或spring.cloud.consul.discovery.management-metadata
属性上填充meta
字段集值。
应用程序.yml
spring:
cloud:
consul:
discovery:
metadata:
myfield: myvalue
anotherfield: anothervalue
上述配置将导致一个服务的元字段包含myfield→myvalue
和anotherfield→anothervalue
。
# 生成的元数据
领事自动注册将自动生成一些条目。
Key | 价值 |
---|---|
'group' | 属性spring.cloud.consul.discovery.instance-group 。只有当instance-group 不为空时,才会生成该值。 |
'secure' | 如果属性spring.cloud.consul.discovery.scheme 等于“HTTPS”,则为真,否则为假。 |
Property spring.cloud.consul.discovery.default-zone-metadata-name , defaults to 'zone' | 属性spring.cloud.consul.discovery.instance-zone 。只有当instance-zone 不为空时,才会生成该值。 |
Spring Cloud Consul 的旧版本通过解析spring.cloud.consul.discovery.tags 属性填充了 Spring Cloud Commons 中的ServiceInstance.getMetadata() 方法。这不再受支持,请迁移到使用spring.cloud.consul.discovery.metadata 映射。 |
---|
# 4.2.5.使 Consul 实例 ID 是唯一的
默认情况下,Consul 实例注册的 ID 等于其 Spring 应用程序上下文 ID。默认情况下, Spring 应用程序上下文 ID 是${spring.application.name}:comma,separated,profiles:${server.port}
。对于大多数情况,这将允许在一台机器上运行一个服务的多个实例。如果需要进一步的唯一性,那么使用 Spring Cloud,你可以通过在spring.cloud.consul.discovery.instanceId
中提供唯一的标识符来覆盖这一点。例如:
应用程序.yml
spring:
cloud:
consul:
discovery:
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
有了这个元数据,以及在 LocalHost 上部署的多个服务实例,随机值就会在其中发挥作用,从而使实例具有唯一性。在 CloudFoundry 中,vcap.application.instance_id
将在 Spring 引导应用程序中自动填充,因此不需要随机值。
# 4.3.查询服务
# 4.3.1.使用负载均衡器
Spring Cloud具有对Feign (opens new window)(一个 REST 客户机构建器)和[[ Spring RestTemplate
](https://DOCS. Spring.io/ Spring-cloud-commons/DOCS/current/reference/html/#rest-template-loadbalancer-client)的支持,用于使用逻辑服务名称/ID 而不是物理 URL 查找服务。Feign 和支持发现的 RESTTemplate 都利用Spring Cloud LoadBalancer (opens new window)实现客户端负载平衡。
如果你想使用 RESTTemplate 访问服务存储,只需声明:
@LoadBalanced
@Bean
public RestTemplate loadbalancedRestTemplate() {
return new RestTemplate();
}
并像这样使用它(注意我们如何使用来自 Consul 的 Stores Service Name/ID,而不是一个完全限定的域名):
@Autowired
RestTemplate restTemplate;
public String getFirstProduct() {
return this.restTemplate.getForObject("https://STORES/products/1", String.class);
}
如果你在多个数据中心中拥有 Consul 集群,并且希望访问另一个数据中心中的服务,那么仅使用服务名称/ID 是不够的。在这种情况下,你使用属性spring.cloud.consul.discovery.datacenters.STORES=dc-west
,其中STORES
是服务名称/ID,dc-west
是存储服务生命的数据中心。
Spring Cloud 现在还提供对Spring Cloud LoadBalancer (opens new window)的支持。 |
---|
# 4.3.2.使用 DiscoveryClient
你也可以使用org.springframework.cloud.client.discovery.DiscoveryClient
,它为非特定于 Netflix 的发现客户端提供了一个简单的 API,例如。
@Autowired
private DiscoveryClient discoveryClient;
public String serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri();
}
return null;
}
# 4.4.领事目录表
领事编目表利用领事的能力手表服务 (opens new window)。Catalog Watch 进行一个阻塞的 Consul HTTP API 调用,以确定是否有任何服务发生了更改。如果有新的服务数据,则会发布心跳事件。
要改变配置手表的频率时,调用 changespring.cloud.consul.config.discovery.catalog-services-watch-delay
。默认值是 1000,以毫秒为单位。延迟是上一次调用结束后和下一次调用开始后的时间量。
要禁用目录手表集spring.cloud.consul.discovery.catalogServicesWatch.enabled=false
。
手表使用 Spring TaskScheduler
来安排与领事的通话。默认情况下,它是一个ThreadPoolTaskScheduler
,其poolSize
为 1。要更改TaskScheduler
,请创建一个类型为TaskScheduler
的 Bean,该类型以ConsulDiscoveryClientConfiguration.CATALOG_WATCH_TASK_SCHEDULER_NAME
常数命名。
# 5.与 Consul 的分布式配置
Consul 提供了键/值存储 (opens new window)用于存储配置和其他元数据。 Spring Cloud Consul Config 是配置服务器和客户端 (opens new window)的一种替代方案。在特殊的“引导”阶段,将配置加载到 Spring 环境中。默认情况下,配置存储在/config
文件夹中。多个PropertySource
实例是根据应用程序的名称和模拟解析属性的 Spring Cloud配置顺序的活动配置文件创建的。例如,一个名为“TestApp”、配置文件为“Dev”的应用程序将创建以下属性源:
config/testApp,dev/
config/testApp/
config/application,dev/
config/application/
最具体的属性源位于顶部,而最不具体的属性源位于底部。config/application
文件夹中的属性适用于使用 Consul 进行配置的所有应用程序。config/testApp
文件夹中的属性仅对名为“TestApp”的服务实例可用。
当前在启动应用程序时读取配置。将 HTTP POST 发送到/refresh
将导致重新加载配置。配置手表还将自动检测更改并重新加载应用程序上下文。
# 5.1.如何激活
要开始使用 Consul 配置,请使用带有组org.springframework.cloud
和工件 IDspring-cloud-starter-consul-config
的启动器。请参阅Spring Cloud Project page (opens new window)以获取有关使用当前 Spring Cloud发布列设置构建系统的详细信息。
# 5.2. Spring Boot Config Data Import
Spring Boot2.4 引入了一种通过spring.config.import
属性导入配置数据的新方法。这是现在从 Consul 获得配置的默认方式。
可选地连接到 Consul,请在应用程序中设置以下属性:
应用程序.属性
spring.config.import=optional:consul:
这将在默认位置“http://localhost:8500”连接到 Consul 代理。如果无法连接到 Consul,删除optional:
前缀将导致 Consul Config 失败。要更改 Consul Config 的连接属性,可以设置spring.cloud.consul.host
和spring.cloud.consul.port
,或者将主机/端口对添加到spring.config.import
语句中,例如,spring.config.import=optional:consul:myhost:8500
。导入属性中的位置优先于主机和端口属性。
Consul Config 将尝试基于spring.cloud.consul.config.name
(默认为spring.application.name
属性的值)和spring.cloud.consul.config.default-context
(默认为application
)从四个自动上下文加载值。如果你希望指定上下文而不是使用计算的上下文,那么可以将该信息添加到spring.config.import
语句中。
application.properties
spring.config.import=optional:consul:myhost:8500/contextone;/context/two
这将可选地只从/contextone
和/context/two
加载配置。
通过spring.config.import 导入 Spring 引导配置数据方法所需的bootstrap 文件(属性或 YAML)是不是。 |
---|
# 5.3.定制
Consul Config 可以使用以下属性进行自定义:
spring:
cloud:
consul:
config:
enabled: true
prefix: configuration
defaultContext: apps
profileSeparator: '::'
如果你已经设置了spring.cloud.bootstrap.enabled=true 或spring.config.use-legacy-processing=true ,或者包含了spring-cloud-starter-bootstrap ,那么上述值将需要放置在bootstrap.yml 中,而不是application.yml 中。 |
---|
enabled
将该值设置为“false”会禁用 Consul Configprefix
设置配置值的基础文件夹defaultContext
设置所有应用程序使用的文件夹名profileSeparator
设置用于在具有配置文件的属性源中分隔配置文件名称的分隔符的值
# 5.4.配置手表
领事配置手表利用领事的能力观看一个键的前缀 (opens new window)。Config Watch 进行一个阻塞的 Consul HTTP API 调用,以确定当前应用程序的任何相关配置数据是否已更改。如果有新的配置数据,将发布刷新事件。这相当于调用/refresh
执行器端点。
要更改配置手表时的频率,请调用 changespring.cloud.consul.config.watch.delay
。默认值是 1000,以毫秒为单位。延迟是上一次调用结束后和下一次调用开始后的时间量。
要禁用配置手表集spring.cloud.consul.config.watch.enabled=false
。
手表使用 Spring TaskScheduler
来安排对领事的拜访。默认情况下,它是一个ThreadPoolTaskScheduler
,其poolSize
为 1。要更改TaskScheduler
,请创建 Bean 类型的TaskScheduler
,该类型的名称为ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME
常数。
# 5.5.具有配置的 YAML 或属性
以 YAML 或 Properties 格式存储一组属性可能更方便,而不是单独的键/值对。将spring.cloud.consul.config.format
属性设置为YAML
或PROPERTIES
。例如,使用 YAML:
spring:
cloud:
consul:
config:
format: YAML
如果你已经设置了spring.cloud.bootstrap.enabled=true 或spring.config.use-legacy-processing=true ,或者包含了spring-cloud-starter-bootstrap ,那么上述值将需要放置在bootstrap.yml 中,而不是application.yml 中。 |
---|
YAML 必须设置在相应的data
键中。使用键上面的默认值将看起来像:
config/testApp,dev/data
config/testApp/data
config/application,dev/data
config/application/data
你可以在上面列出的任何键中存储 YAML 文档。
你可以使用spring.cloud.consul.config.data-key
更改数据键。
# 5.6.Git2config 领事
Git2Consul 是一个 Consul 社区项目,它将文件从 Git 存储库加载到 Consul 中的各个密钥。默认情况下,键的名称是文件的名称。YAML 和 Properties 文件分别支持.yml
和.properties
的文件扩展名。将spring.cloud.consul.config.format
属性设置为FILES
。例如:
bootstrap.yml
spring:
cloud:
consul:
config:
format: FILES
给定/config
中的以下键,development
配置文件和foo
的应用程序名称:
.gitignore
application.yml
bar.properties
foo-development.properties
foo-production.yml
foo.properties
master.ref
将创建以下财产来源:
config/foo-development.properties
config/foo.properties
config/application.yml
每个键的值需要是一个格式正确的 YAML 或 Properties 文件。
# 5.7.快速失败
在某些情况下(如本地开发或某些测试场景),如果 Consul 不能用于配置,那么不失败可能是很方便的。设置spring.cloud.consul.config.fail-fast=false
将导致配置模块记录警告,而不是抛出异常。这将允许应用程序继续正常启动。
如果你已经设置了spring.cloud.bootstrap.enabled=true 或spring.config.use-legacy-processing=true ,或者包含了spring-cloud-starter-bootstrap ,那么上述值将需要放置在bootstrap.yml 中,而不是application.yml 中。 |
---|
# 6.执政官重试
如果你预计,当你的应用程序启动时,Consul Agent 可能会偶尔无法使用,那么你可以要求它在出现故障后继续尝试。你需要在 Classpath 中添加spring-retry
和spring-boot-starter-aop
。默认的行为是重试 6 次,初始退避间隔为 1000ms,后续退避的指数乘数为 1.1。你可以使用spring.cloud.consul.retry.*
配置属性来配置这些属性(以及其他属性)。这对 Spring Cloud Consul Config 和 Discovery 注册都有效。
若要完全控制重试,请添加@Bean 类型的RetryOperationsInterceptor ,ID 为“consulretryInterceptor”。 Spring 重试有一个 RetryInterceptorBuilder ,这使得很容易创建一个。 |
---|
# 7. Spring Cloud Bus with Consul
# 7.1.如何激活
要开始使用 Consul 总线,请使用分组org.springframework.cloud
和工件 IDspring-cloud-starter-consul-bus
的启动器。请参阅Spring Cloud Project page (opens new window)以获取有关使用当前 Spring Cloud发布列设置构建系统的详细信息。
有关可用的执行器端点和 HOWTO 发送自定义消息,请参见Spring Cloud Bus (opens new window)文档。
# 8.带 hystrix 的断路器
应用程序可以通过在 POM.xml:spring-cloud-starter-hystrix
项目中包含此启动器来使用 Spring Cloud Netflix 项目提供的 Hystrix 断路器。Hystrix 并不依赖于 Netflix 的 Discovery 客户端。@EnableHystrix
注释应该放在配置类(通常是主类)上。然后可以用@HystrixCommand
注释方法来由断路器进行保护。有关更多详细信息,请参见文件 (opens new window)。
# 9.基于涡轮机和 consul 的 hystrix 度量聚合
Turbine(由 Spring Cloud Netflix 项目提供)聚合了多个实例 Hystrix Metrics 流,因此仪表板可以显示聚合视图。Turbine 使用DiscoveryClient
接口来查找相关实例。要使用具有 Spring Cloud领事的涡轮机,以类似于以下示例的方式配置涡轮机应用程序:
POM.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
请注意,涡轮机依赖项不是启动器。涡轮启动器包括对 Netflix Eureka 的支持。
application.yml
spring.application.name: turbine
applications: consulhystrixclient
turbine:
aggregator:
clusterConfig: ${applications}
appConfig: ${applications}
clusterConfig
和appConfig
节必须匹配,因此将以逗号分隔的服务 ID 列表放入单独的配置属性中非常有用。
Turbine.java
@EnableTurbine
@SpringBootApplication
public class Turbine {
public static void main(String[] args) {
SpringApplication.run(DemoturbinecommonsApplication.class, args);
}
}
# 10.配置属性
要查看所有 consul 相关配置属性的列表,请检查附录页。