3. Zookeeper服务注册与发现
注册中心Zookeeper
- zookeeper是一个分布式协调工具,可以实现注册中心功能
- 关闭Linux服务器防火墙后启动zookeeper服务器
- zookeeper服务器取代Eureka服务器,zk作为服务注册中心
服务提供者
-
新建cloud-provider-payment8004
-
POM
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency>
-
YML
server: port: 8004 spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 192.168.136.140:2181
-
主启动类
@SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
-
Controller
@RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/zk") public String paymentzk(){ return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString(); } }
-
在linux系统中启动zookeeper服务器
./zkServer.sh start
,连接./zkCli.sh
-
启动8004注册进zookeeper
-
出现jar包冲突3.5.3和3.4.9,修改pom文件
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zk 3.4,9版本--> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
-
linux命令
ls /
后出现注册进的服务
-
-
思考
- 服务节点是临时节点还是持久节点?临时结点
服务消费者
- 新建cloud-consumerzk-order80
- POM
- YML
- 主启动
- 业务类
- 启动8004注册进zookeeper
- 访问测试地址