1. 准备工作
-
在 Linux 上部署 Zookeeper
我找的教程,也可以自行寻找
https://www.cnblogs.com/zhiyouwu/p/11546097.html
-
如果 Zookeeper 部署在 Linux 上,先关闭防火墙
systemctl stop firewalld
-
开启 Zookeeper
./zkServer.sh start
-
开启 Zookeeper 后,查看 ip
ifconfig
-
ping 一下 Windows 查看是否连通
-
最后打开 Zookeeper 客户端
./zkCli.sh
2. 服务提供者 Provider
-
新建一个模块 :
cloud-provider-payment-8004
-
修改 POM 文件
<dependencies> <dependency> <groupId>com.demo.springcloud</groupId> <artifactId>cloud-api-commons</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>
-
编写 YML
server: port: 8004 spring: application: name: cloud-provider-payment # 这里和之前不同 cloud: zookeeper: # 这里要换成自己 Liunx 系统的 IP connect-string: 192.168.142.120:2181
-
编写主启动类
@SpringBootApplication @EnableDiscoveryClient public class ZookeeperMain8004 { public static void main(String[] args) { SpringApplication.run(ZookeeperMain8004.class, args); } }
-
编写业务类
-
Controller
@Slf4j @RestController 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(); } }
-
启动 Zookeeper
-
启动 8004
-
解决办法
<!-- 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> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zk 对应的版本,按照自己的 Zookeeper 版本来--> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.11</version> </dependency>
-
重启 8004,没有报错成功启动
-
去 Zookeeper 上查看,是否注册进去
访问 :http://localhost:8004/payment/zk
3. 服务消费者 Consumer
-
新建一个模块 :
cloud-consumer-order-ZK-80
-
修改 POM 文件
<dependencies> <dependency> <groupId>com.demo.springcloud</groupId> <artifactId>cloud-api-commons</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> <!--排除zk3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zk 对应的版本,按照自己的 Zookeeper 版本来--> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.11</version> </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>
-
编写 YML
server: port: 80 spring: application: name: cloud-consumer-order cloud: zookeeper: connect-string: 192.168.142.120:2181
-
编写主启动类
@SpringBootApplication @EnableDiscoveryClient public class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class, args); } }
-
编写业务类
-
配置 RestTemplate 的 Bean
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
-
编写 Controller
@Slf4j @RestController public class OrderZKController { public static final String INVOME_URL = "http://cloud-provider-payment"; @Autowired private RestTemplate restTemplate; @GetMapping("/consumer/payment/zk") public String payment (){ String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class); return result; } }
-
启动测试
Zookeeper
访问 http://localhost/consumer/payment/zk