|
该版本仍在开发中,尚未被视为稳定。最新稳定版请使用Spring Cloud Consul 5.0.0! |
与领事的服务发现
如何激活
要激活领事服务发现,请使用带组的起始工具org.springframework.cloud以及工件ID春云启动者 领事.请参阅 Spring Cloud Project 页面,了解如何使用当前的 Spring Cloud 发布列车来设置你的构建系统。
向领事登记
当客户端向Consul注册时,它会提供关于自身的元数据,如主机和端口、ID、名称和标签。默认情况下会创建一个HTTP Check,确保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);
}
}
(即完全正常的Spring Boot应用)。如果领事客户位于其他地方本地主持人:8500,配置是定位客户端的必要条件。例:
spring:
cloud:
consul:
host: localhost
port: 8500
如果你使用 Spring Cloud Consul 配置,并且你已经设置了spring.cloud.bootstrap.enabled=true或Spring.config.use-legacy-processing=true或使用Spring-cloud-starter-bootstrap那么上述数值需要被置于bootstrap.yml而不是application.yml. |
默认服务名称、实例ID和端口,取自环境是${spring.application.name}, Spring Context ID 和${server.port}分别。
要禁用领事发现客户端,你可以设置spring.cloud.consul.discovery.enabled自false.领事发现客户端也会被禁用spring.cloud.discovery.enabled设置为false.
要禁用服务注册,可以设置spring.cloud.consul.discovery.register(春云.consul.discovery.register)自false.
将管理注册为独立服务
当管理服务器端口设置为与应用端口不同时,通过设置management.server.port物业管理服务将作为与申请服务不同的服务注册。例如:
spring:
application:
name: myApp
management:
server:
port: 4452
上述配置将注册以下两项服务:
-
申请服务:
ID: myApp Name: myApp
-
管理服务:
ID: myApp-management Name: myApp-management
管理服务将会继承其实例ID和服务名称来自申请服务。例如:
spring:
application:
name: myApp
management:
server:
port: 4452
spring:
cloud:
consul:
discovery:
instance-id: custom-service-id
serviceName: myprefix-${spring.application.name}
上述配置将注册以下两项服务:
-
申请服务:
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
HTTP 健康检查
Consul实例的健康检查默认为“/actuator/health”,这是Spring Boot Actuator应用程序中健康端点的默认位置。即使是执行器应用,如果你使用非默认的上下文路径或 servlet 路径(例如server.servletPath=/foo)或管理端点路径(例如:management.server.servlet.context-path=/admin).
Consul用于检查健康端点的区间也可以配置。“10秒”和“1米”分别代表10秒和1分钟。
这个例子说明了上述情况(参见spring.cloud.consul.discovery.health-check-*附录页面中的属性有更多选项)。
spring:
cloud:
consul:
discovery:
healthCheckPath: ${management.server.servlet.context-path}/actuator/health
healthCheckInterval: 15s
你可以通过设置完全禁用HTTP健康检查spring.cloud.consul.discovery.register-health-check=false.
应用头部
头部可以应用于健康检查请求。例如,如果你试图注册一个使用 Vault 后端的 Spring Cloud 配置服务器:
spring:
cloud:
consul:
discovery:
health-check-headers:
X-Config-Token: 6442e58b-d1ea-182e-cfa5-cf9cddef0722
根据HTTP标准,每个头部可以有多个值,此时可以提供一个数组:
spring:
cloud:
consul:
discovery:
health-check-headers:
X-Config-Token:
- "6442e58b-d1ea-182e-cfa5-cf9cddef0722"
- "Some other value"
TTL健康检查
可以使用 Consul TTL 检查代替默认配置的 HTTP 检查。 主要区别在于应用程序向领事代理发送心跳信号,而领事代理向应用程序发送请求。
应用程序发送ping的间隔也可以被配置。“10秒”和“1米”分别代表10秒和1分钟。 默认是30秒。
这个例子说明了上述情况(参见spring.cloud.consul.discovery.heartbeat.*附录页面中的属性有更多选项)。
spring:
cloud:
consul:
discovery:
heartbeat:
enabled: true
ttl: 10s
TTL 应用状态
对于 Spring Boot 执行器应用,状态由其可用健康端点确定。 当健康端点不可用(无论是被禁用还是不是 Spring Boot Actuator 应用程序)时,它会假设该应用处于良好状态。
查询健康端点时,默认使用根健康组。 通过设置以下属性,可以使用不同的健康组:
spring:
cloud:
consul:
discovery:
heartbeat:
actuator-health-group: <your-custom-group-goes-here>
你可以通过设置以下属性完全禁用健康端点的使用:
spring:
cloud:
consul:
discovery:
heartbeat:
use-actuator-health: false
自定义TTL应用状态
如果你想配置自己的应用状态机制,只需实现ApplicationStatusProvider接口
@Bean
public class MyCustomApplicationStatusProvider implements ApplicationStatusProvider {
public CheckStatus currentStatus() {
return yourMethodToDetermineAppStatusGoesHere();
}
}
并使其对应用上下文开放:
@Bean
public CustomApplicationStatusProvider customAppStatusProvider() {
return new MyCustomApplicationStatusProvider();
}
执行器健康指示器
如果服务实例是Spring Boot执行器应用程序,可能会提供以下执行器健康指示。
发现客户健康指标
当Consul服务发现激活时,会配置DiscoverClientHealthIndicator,并向执行器健康端点开放。 这里有配置选项。
领事健康指标
配置了一个指示器以验证领事客户.
默认情况下,它会检索领事领导节点状态和所有注册服务。
在拥有多个注册服务的部署中,每次健康检查都检索所有服务可能成本较高。
跳过服务检索,只检查领先节点状态设置Spring.cloud.consul.health-indicator.include-services-query=false.
要禁用指示器集管理.健康.consul.enabled=false.
| 当应用程序运行在引导上下文模式(默认模式)时, 该指示器加载到引导框架上下文中,不提供给执行器健康端点。 |
元数据
Consul 支持服务的元数据。春云的ServiceInstance有Map<String, String>元数据从服务中填充的字段元田。为了填充元场集合值spring.cloud.consul.discovery.metadata或Spring.cloud.consul.discovery.management-metadata性能。
spring:
cloud:
consul:
discovery:
metadata:
myfield: myvalue
anotherfield: anothervalue
上述配置将生成一个服务,其元字段包含myfield→myvalue和另一个字段→另一个值.
生成元数据
领事自动注册会自动生成一些条目。
| 钥匙 | 值 |
|---|---|
“群体” |
属性 |
“安全” |
如果性质为真 |
属性 |
属性 |
Spring Cloud Consul的旧版本曾在ServiceInstance.getMetadata()通过解析 Spring Cloud Commons 的方法spring.cloud.consul.discovery.tags财产。该功能已不再支持,请迁移至使用spring.cloud.consul.discovery.metadata地图。 |
使领事实例ID唯一
默认情况下,consul实例注册的ID等于其Spring Application Context ID。默认情况下,Spring 应用上下文 ID 为${spring.application.name}:逗号,separated, profiles:${server.port}.在大多数情况下,这允许同一服务的多个实例在一台机器上运行。如果需要更多的唯一性,使用 Spring Cloud 可以通过在 中提供唯一标识符来覆盖此限制spring.cloud.consul.discovery.instanceId.例如:
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 Boot 应用中自动填充,因此不需要随机值。
查找服务
使用负载均衡器
Spring Cloud 支持 Feign(一个 REST 客户端构建器),还有SpringRest模板用逻辑服务名称/ID查找服务,而不是物理URL。Feign 和发现感知型 RestTemplate 都利用 Spring Cloud LoadBalancer 进行客户端负载均衡。
如果你想使用 RestTemplate 访问服务 STORES,只需声明:
@LoadBalanced
@Bean
public RestTemplate loadbalancedRestTemplate() {
return new RestTemplate();
}
并这样使用(注意我们用Consul的STORES服务名称/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哪里商店是服务名称/ID 和华盛顿特区西区是数据中心
STORES服务所在地。
| Spring Cloud 现在也支持 Spring Cloud 负载均衡器。 |
使用 DiscoveryClient
你也可以使用org.springframework.cloud.client.discovery.DiscoveryClient它为发现客户端提供一个简单的 API,且不限于 Netflix,例如
@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;
}
领事目录表
领事目录表利用领事监控服务的能力。目录监视会调用阻塞性的Consul HTTP API以确定是否有服务发生变化。如果有新的服务数据,会发布心跳事件。
要更改配置手表被调用的频率,请更改Spring.cloud.consul.config.discovery.catalog-services-watch-delay.默认值是1000,单位是毫秒。延迟是指上一次召唤结束后与下一次召唤开始之间的时间。
禁用目录表集spring.cloud.consul.discovery.catalogServicesWatch.enabled=false.
这款手表使用了发条任务调度器安排与领事通话。默认情况下,它是ThreadPoolTaskScheduler其中池大小1。要更改任务调度器,创建 的 bean任务调度器以ConsulDiscoveryClientConfiguration.CATALOG_WATCH_TASK_SCHEDULER_NAME不断。