|
对于最新稳定版本,请使用 Spring Cloud Zookeeper 5.0.1! |
服务发现与Zookeeper
激活
Including a dependency on
org.springframework.cloud:spring-cloud-starter-zookeeper-discovery enables
autoconfiguration that sets up Spring Cloud Zookeeper Discovery.
对于网页功能,你仍然需要包含
org.springframework.boot:spring-boot-starter-web。 |
| 当使用 Zookeeper 3.4 版本时,需要更改 包含依赖的方式,如在 此处 所描述。 |
注册Zookeeper
当客户端向Zookeeper注册时,它会提供关于自身的元数据(如主机和端口、ID和名称)。
以下示例显示了一个Zookeeper客户端:
@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 应用程序。 |
如果Zookeeper位于非localhost:2181处,配置必须 提供服务器位置的配置,如以下示例所示:
spring:
cloud:
zookeeper:
connect-string: localhost:2181
如果使用 Spring Cloud Zookeeper Config,前面示例中显示的值应放在 bootstrap.yml 中而不是 application.yml。 |
The default service name, instance ID, and port (taken from the Environment) are
${spring.application.name}, the Spring Context ID, and ${server.port}, respectively.
具有在类路径上具有spring-cloud-starter-zookeeper-discovery的配置会使应用程序同时成为Zookeeper的“服务”(也就是说,它会注册自己)和“客户端”(也就是说,它可以查询Zookeeper以定位其他服务)。
如果您希望禁用Zookeeper Discovery Client,可以将spring.cloud.zookeeper.discovery.enabled设置为false。
使用发现客户端
Spring Cloud 支持 OpenFeign (一个 REST 客户端构建器),通过 Spring Cloud Loadbalancer 提供,使用逻辑服务名称代替物理URL。
您也可以使用 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().toString();
}
return null;
}