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(); } }
可以實現輪詢