四、SpringCloud学习笔记之Zookeeper服务注册与发现
2020-07-30 12:31
393 查看
Zookeeper服务注册与发现
Eureka停止更新了你怎么办:
https://github.com/Netflix/eureka/wiki
1、使用docker安装zookeeper
下载镜像:
docker pull zookeeper:3.4.9
启动容器并添加映射:
docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:3.4.9
查看是否启动:
SpringCloud整合Zookeeper代替Eureka
注册中心Zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
服务提供者
建model》改pom》写yml》主启动
1、model
改pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud</artifactId> <groupId>com.liang.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-zkpayment-8003</artifactId> <dependencies> <dependency> <groupId>com.liang.springcloud</groupId> <artifactId>common</artifactId> <version>${project.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
写yml:
server: port: 8003 spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 192.168.0.6:2181
主启动:
package com.liang.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ZkPaymentApplication { public static void main(String[] args) { SpringApplication.run(ZkPaymentApplication.class,args); } }
Controller:
package com.liang.cloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @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(); } }
启动8003注册进zookeeper:
启动后问题
解决zookeeper版本jar包冲突问题:
修改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>
访问
http://localhost:8003/payment/zk
验证测试:
连接
docker exec -it 53a zkCli.sh
ls /
ls /service
这里node的名字就是yml配置的springapplication的名字
验证测试2:
ls /services/cloud-provider-payment
ls /services/cloud-provider-payment/a855f6d9-cb61-478e-919d-660d62e9f951
get /services/cloud-provider-payment/a855f6d9-cb61-478e-919d-660d62e9f951
思考:
服务节点是临时节点还是持久节点
是临时节点。
服务消费者
model、pom、yml同上
配置RestTemplate:
package com.liang.cloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class MyConfig { @LoadBalanced @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
controller:
package com.liang.cloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class OrderController { public static final String INVOME_URL = "http://cloud-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/zk") public String payment (){ String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class); return result; } }
启动80注册进zookeeper
访问:
http://localhost/consumer/payment/zk
相关文章推荐
- SpringCloud 学习笔记 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
- 五、SpringCloud学习笔记之Consul服务注册与发现
- SpringCloud 学习笔记------服务的注册和发现
- Spring Cloud 学习笔记-Eureka 服务注册与发现
- Spring Cloud 学习笔记一 服务注册与发现
- Spring cloud学习笔记1-服务注册中心Eureka
- SpringCloud 学习 | 第一篇: 高可用的服务注册与发现(Eureka)
- [spring cloud学习3]spring cloud服务注册与发现
- Spring Cloud 学习之旅 --- 服务注册与发现(一)
- springcloud学习之旅(一)服务的注册于发现
- 微服务笔记之Spring Cloud 服务的注册与发现(Eureka)
- spring cloud Eureka 服务注册与服务发现的学习(1)
- 微服务SpringCloud开弓之Zookeeper服务注册与发现《四》
- spring-cloud学习(一)———eureka集群服务注册于与发现
- SpringCloud 学习记录(一):服务注册与发现(eureka+feign)
- Spring Cloud 学习之旅 --- 服务注册与发现(二)
- eclipse springcloud服务发现服务注册(Eureka) 学习日志
- spring-cloud项目学习与实践记录之——服务注册与发现
- 学习笔记:微服务-2.spring cloud Eureka server 注册中心搭建
- 使用Spring Cloud Zookeeper实现服务的注册和发现