ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
步骤如下: **1. 构建消费端模块:cloud-comsumerzk-order80** **2. 当前模块添加 zookeeper-discovery 依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> ... </dependencies> ``` **3. 在当前模块的`application.yml`配置 zookeeper 相关** ```yml server: port: 80 spring: application: name: cloud-comsumer-order cloud: zookeeper: #zookeeper地址 connect-string: 192.168.1.17:2181 ``` **4. 在当前启动类上标注注解`@EnableDiscoveryClient`** ```java @SpringBootApplication @EnableDiscoveryClient public class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class, args); } } ``` **5. 在当前模块注入 RestTemplate 组件方便访问服务端** ```java @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } } ``` **6. 当前模块的 controller 层** ```java @RestController @RequestMapping("/comsumer") public class OrderController { @Resource private RestTemplate restTemplate; //cloud-payment-service 为服务端的 spring.application.name 配置 public static final String PAYENT_URL = "http://cloud-payment-service"; @GetMapping("/payment/zk") public String getpaymentInfo() { String res = restTemplate.getForObject(PAYENT_URL + "/payment/zk", String.class); return res; } } ``` **7. 测试** (1)启动 Zookeeper。 ```shell # zkServer.sh start # jps 2621 QuorumPeerMain --出现该进行则启动成功 ``` 记住需要到 Linux 中关闭防火墙,外面应用才可以访问到 Zookeeper。 ```shell -- 关闭防火墙 # systemctl stop firewalld --查看防火墙是否关闭 # firewall-cmd --state not running -- 禁止防火墙开机自启动 # systemctl disable firewalld.service ``` (2)启动服务端模块、消费端模块。 访问消费端模块:http://localhost:80/comsumer/payment/zk 。得到如下响应。 ``` serverPort:8004 ``` (3)到 Zookeeper 中查看消费端的注册信息。 ```shell --切换到 Zookeeper 的安装目录 # cd /opt/install/zookeeper --启动 zookeeper 客户端工具 # bin/zkCli.sh --查看 zookeeper 节点 [zk: localhost:2181(CONNECTED) 16] ls / [services, zookeeper] [zk: localhost:2181(CONNECTED) 17] ls /services [cloud-comsumer-order, cloud-payment-service] [zk: localhost:2181(CONNECTED) 18] ls /services/cloud-comsumer-order [476aad5c-4f02-44b2-b42d-741bea0ed116] --获取节点信息 [zk: localhost:2181(CONNECTED) 19] get /services/cloud-comsumer-order/476aad5c-4f02-44b2-b42d-741bea0ed116 {"name":"cloud-comsumer-order","id":"476aad5c-4f02-44b2-b42d-741 ``` 完整的注册信息如下: ```json { "name":"cloud-comsumer-order", "id":"476aad5c-4f02-44b2-b42d-741bea0ed116", "address":"USER-20210829ES", "port":80, "sslPort":null, "payload":{ "@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance", "id":"application-1", "name":"cloud-comsumer-order", "metadata":{ } }, "registrationTimeUTC":1637399316399, "serviceType":"DYNAMIC", "uriSpec":{ "parts":[ { "value":"scheme", "variable":true }, { "value":"://", "variable":false }, { "value":"address", "variable":true }, { "value":":", "variable":false }, { "value":"port", "variable":true } ] } } ```