企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
步骤如下: **1. 构建服务端模块:cloud-provider-payment8004** **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: 8004 spring: application: name: cloud-payment-service cloud: zookeeper: #zookeeper地址 connect-string: 192.168.1.17:2181 ``` **4. 在当前启动类上标注注解`@EnableDiscoveryClient`** ```java @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class, args); } } ``` **5. 当前模块的 controller 层** ```java @RestController @RequestMapping("/payment") public class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping("/zk") public String paymentzk() { return "serverPort:" + serverPort; } } ``` **6. 测试** (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:8004/payment/zk 。得到如下响应。 ``` serverPort:8004 ``` (3)到 Zookeeper 中查看注册信息。 ```shell --切换到 Zookeeper 的安装目录 # cd /opt/install/zookeeper --启动 zookeeper 客户端工具 # bin/zkCli.sh --查看 zookeeper 节点 [zk: localhost:2181(CONNECTED) 11] ls / [services, zookeeper] [zk: localhost:2181(CONNECTED) 12] ls /services [cloud-payment-service] [zk: localhost:2181(CONNECTED) 13] ls /services/cloud-payment-service [8ca8dad9-f3b0-4289-baba-ce93cec209e8] [zk: localhost:2181(CONNECTED) 14] ls /services/cloud-payment-service/8ca8dad9-f3b0-4289-baba-ce93cec209e8 [] --获取节点信息 [zk: localhost:2181(CONNECTED) 15] get /services/cloud-payment-service/8ca8dad9-f3b0-4289-baba-ce93cec209e8 {"name":"cloud-payment-service","id":"8ca8dad9-f3b0-4289-... ``` 完整的注册信息如下: ```json { "name":"cloud-payment-service", "id":"8ca8dad9-f3b0-4289-baba-ce93cec209e8", "address":"USER-20210829ES", "port":8004, "sslPort":null, "payload":{ "@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance", "id":"application-1", "name":"cloud-payment-service", "metadata":{ } }, "registrationTimeUTC":1637397943486, "serviceType":"DYNAMIC", "uriSpec":{ "parts":[ { "value":"scheme", "variable":true }, { "value":"://", "variable":false }, { "value":"address", "variable":true }, { "value":":", "variable":false }, { "value":"port", "variable":true } ] } } ```