Spring Cloud之整合ZK作为注册中心
Eureka已经闭源了,用zk可以替代之
Eureka 作为注册中心
Dubbo也是zk作为注册中心的
Zookeeper简介
Zookeeper是一个分布式协调工具,可以实现服务注册与发现、注册中心、消息中间件、分布式配置中心等。
公共pom:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot整合eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
package com.toov5.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务 public class zkMemberApiController { @Value("${server.port}") private String serverPort; @RequestMapping("/getMember") public String getMember() { return "会员服务prot:"+serverPort; } public static void main(String[] args) { SpringApplication.run(zkMemberApiController.class, args); } }
###订单服务的端口号
server:
port: 8003
###服务别名----服务注册到注册中心名称
spring:
application:
name: zk-member
cloud:
zookeeper:
connect-string: 192.168.91.7:2181
启动后: yml配置文件的别名
通过json解析工具:
{ "name": "zk-member", "id": "c01a3167-71c4-4d8a-8584-332c659e2d57", "address": "localhost", "port": 8002, "sslPort": null, "payload": { "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance", "id": "application-1", "name": "zk-member", "metadata": {} }, "registrationTimeUTC": 1542086637317, "serviceType": "DYNAMIC", "uriSpec": { "parts": [{ "value": "scheme", "variable": true }, { "value": "://", "variable": false }, { "value": "address", "variable": true }, { "value": ":", "variable": false }, { "value": "port", "variable": true }] } }
yml
###订单服务的端口号 server: port: 8002 ###服务别名----服务注册到注册中心名称 spring: application: name: zk-order cloud: zookeeper: connect-string: 192.168.91.7:2181
controller
package com.toov5.api.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @SpringBootApplication @EnableDiscoveryClient //如果服务使用consul或者zk使用这个注解 向注册中心注册服务 public class zkOrderApiController { @Value("${server.port}") private String serverPort; @Autowired private RestTemplate restTemplate; @RequestMapping("/orderToMember") public String orderToMember() { String url ="http://zk-member/getMember"; return restTemplate.getForObject(url, String.class); } public static void main(String[] args) { SpringApplication.run(zkOrderApiController.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
可以实现轮询