官網:
https://spring.io/
更多spring cloud zookeeper 參考
https://docs.spring.io/spring-cloud-zookeeper/docs/current/reference/html


左側選單 向下找到 spring Cloud Zookeeper


所有我們希望看到的都在 Reference Doc 中,點擊進入
連接zookeeper服務器
First, run Zookeeper on your machine. Then you can access it and use it as a Service Registry and Configuration source with Spring Cloud Zookeeper.
首先,安裝zookeeper ,然后 就可以利用spring cloud zookeeper 把zookeeper服務器,當做注冊服務器訪問
在程式中要啟用zookeeper可以在spring boot 程式中依賴 spring-cloud-zookeeper-core and spring-cloud-zookeeper-discovery 來實作,
The most convenient way to add the dependency is with a Spring Boot starter: org.springframework.cloud:spring-cloud-starter-zookeeper-discovery.
但是最方便的的方式是 依賴 spring-cloud-starter-zookeeper-discovery.
修改zookeeper服務器地址
When this HTTP server runs, it connects to Zookeeper, which runs on the default local port (2181). To modify the startup behavior, you can change the location of Zookeeper by using application.properties, as shown in the following example:
spring:
cloud:
zookeeper:
connect-string: localhost:2181
從zookeeper獲取資料
You can now use DiscoveryClient, @LoadBalanced RestTemplate, or @LoadBalanced WebClient.Builder to retrieve services and instances data from Zookeeper, as shown in the following example:
@Autowired
private DiscoveryClient discoveryClient;
public String serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri().toString();
}
return null;
}
服務提供者(provider)Pom.xml
<?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>springcloud19</artifactId> <groupId>com.hztech</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consumer-zk-order80</artifactId> <properties> <maven.compiler.source>19</maven.compiler.source> <maven.compiler.target>19</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <zookeeper.version>4.0.0</zookeeper.version> </properties> <dependencies> <!-- zookeeper client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-zookeeper-discovery</artifactId> <version>${zookeeper.version}</version> </dependency> <!-- 引入公用模塊--> <dependency> <groupId>com.hztech</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
服務提供者(provider)application.yml

服務提供者(provider) Main()
@SpringBootApplication @EnableDiscoveryClient public class PaymentZkMain8011 { public static void main(String[] args) { SpringApplication.run( PaymentZkMain8011.class, args); } }
運行效果
啟動程式

登錄zookeeper客戶端( bin/zkCli.sh -server IP)

可以看到服務已經成功注冊到zookeeper服務中
讀取資料
[zk: machine136(CONNECTED) 4] get /services/provider-zk-payment/6ab8deaf-b6bd-4597-84fb-41d991c24636
{"name":"provider-zk-payment","id":"6ab8deaf-b6bd-4597-84fb-41d991c24636","address":"localhost","port":8011,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"provider-zk-payment","name":"provider-zk-payment","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1676184308283,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
[zk: machine136(CONNECTED) 5]
測驗業務

用同樣的方式創建第二個服務提供者模塊(port:8012),并啟用 客戶端發現注解
啟動后登錄zookeeper client 查看注冊的的服務 payment-zk-provider

已經可看到兩個服務器在線了
創建消費模塊(port:99) 完成對provider的呼叫,并完成負載均衡
1、mven 普通專案
2、添加依賴
<dependencies> <!-- zookeeper client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.7.1</version> </dependency> <!-- 引入公用模塊--> <dependency> <groupId>com.hztech</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
3、application.yml
server:
port: 99
spring:
application:
name: order-Service-zk
cloud:
zookeeper:
connect-string: 192.168.1.136:2181,192.168.1.137:2181,192.168.1.138:2181 # zk地址 192.168.1.x是linux zookeeper服務地址
4、main()方法
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableDiscoveryClient public class OrderZkMain80 { public static void main(String[] args) { SpringApplication.run( OrderZkMain80.class, args); } }
5、創建restTemplate Bean
@Configuration public class AppContextConfig { @Bean @LoadBalanced public RestTemplate getRestTmp() { return new RestTemplate(); } }
注意:
controller中的請求地址 直接為服務名稱,地址和埠交給zookeeper +restTemplate 完成轉換
http://provider-zk-payment
6、啟用專案 并驗證
服務注冊

介面呼叫
第一次請求

第二次請求

結束
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543669.html
標籤:其他
