官网:

https://spring.io/

左侧菜单 向下找到 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、启用项目 并验证

服务注册

接口调用

第一次请求

第二次请求

结束

最新文章

  1. 理解CSS视觉格式化
  2. Array方法
  3. C register
  4. java学习:ArrayList的实现及原理
  5. AX Dynamic 2012 SSRS autorepot中取当前公司名、打印时间、打印页码
  6. Mybatis多参传递的四种解决方案
  7. Python开发之【简单计算器】
  8. 请求php返回json生成自定义对象
  9. CSS制作图片水平垂直居中 亲测推荐
  10. unity3d切换场景时,背景音乐保持播放
  11. 速卖通api--发起授权
  12. JavaScript插件 Bootstrap自带了13个jQuery插件,这些插件为Bootstrap中的组件赋予了“生命”
  13. Codeforces Round #346 (Div. 2) A Round-House
  14. Go 从入门到精通(三)字符串,时间,流程控制,函数
  15. 关于微信小程序appsecret保护的问题
  16. ASP.NET代码调用SQL Server带DateTime类型参数的存储过程抛出异常问题
  17. HashMap 和 Hashtable 的 6 个区别,最后一个没几个人知道!
  18. RF运行脚本报错:WebDriverException: Message: unknown error: call function result missing
  19. cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
  20. Redis4.0新特性之-大KEY删除

热门文章

  1. Vue 基础学习总结
  2. 详解Native Memory Tracking之追踪区域分析
  3. 第2-3-7章 个人网盘服务接口开发-文件存储服务系统-nginx/fastDFS/minio/阿里云oss/七牛云oss
  4. c3 linearization详解
  5. 关于python函数传参
  6. 云原生 • Kubernetes 认识 k8s、k8s 架构、核心概念点介绍
  7. python3中的常见知识点3------reduce()函数
  8. Github Actions 学习笔记
  9. 前缀树(Tire)—Python
  10. 手动解析word Table模块内容