Zookeeper服务注册与发现
SpringCloud整合Zookeeper代替Eureka。,解决zookeeper依赖冲突问题
·
Zookeeper服务注册与发现
SpringCloud整合Zookeeper代替Eureka
前提条件:
- 虚拟机安装了Zookepper服务并关闭防火墙(也可以使用win版本的)
- 使用虚拟机需要连接网络与本机互通才能调用
1、ZooKeeper服务提供者
1、创建cloud-provider-payment8004服务提供者
2、修改pom.xml依赖文件
SpringBoot整合zookeeper客户端是新的其他的不变,替换Eureka依赖
<?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>springcloud2022</artifactId>
<groupId>com.zcl.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.zcl.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
3、添加YAML配置文件
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 127.0.0.1:2181 # Zookeeper连接地址
4、创建启动类
注意:下面的注解又一次的使用到
@EnableDiscoveryClient
,该注解用于向使用consul或者zookeeper作为注册中心时注册服务
package com.zcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 描述:启动类
*
* @author zhong
* @date 2022-09-18 20:25
*/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
5、控制器编写
package com.zcl.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* 描述:服务提供者控制器
*
* @author zhong
* @date 2022-09-18 20:29
*/
@RestController
@RequestMapping("/payment")
public class PaymentController {
/**
* 获取yaml配置端口号
*/
@Value("${server.port}")
private String serverPort;
/**
* 前端访问接口返回端口和UUID
* @return
*/
@RequestMapping(value = "zk")
public String paymentzk()
{
return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
6、启动ZooKeeper服务中心【3.5.6】
-
虚拟机启动
# 启动服务端 ./zkServer.sh # 启动客户端 ./zkCli.sh
-
win启动
双击zkServer.cmd启动服务端 双击zkCli.cmd启动客户端
启动客户端连接服务端
-
查看zookeeper服务命令
查看根目录服务
ls /
7、解决jar包冲突问题
-
查看jar包
-
出现问题
- 自己带的ar包版本与zookeeper版本不一致不一致
-
排除冲突依赖
<!-- SpringBoot整合zookeeper客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--排除自身携带的3.5.3-beta--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--引入与zookeeper服务端版本一致的版本--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.6</version> </dependency>
8、zookeeper客户端查看以及内容
服务名称就是
application,yaml
配置文件里面的spring: application: name: cloud-provider-payment
结果
{
"name": "cloud-provider-payment",
"id": "d6b9cb33-2c82-4488-a362-dbb9961e6349",
"address": "localhost",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {
}
},
"registrationTimeUTC": 1663505810627,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [
{
"value": "scheme",
"variable": true
},
{
"value": "://",
"variable": false
},
{
"value": "address",
"variable": true
},
{
"value": ":",
"variable": false
},
{
"value": "port",
"variable": true
}
]
}
}
9、浏览器访问控制器查看输出
http://localhost:8004/payment/zk
10、临时节点还是持久节点
每一个微服务作为一个节点放到zookeeper里面,并且是临时的
测试:
关闭8004服务提供者,通过下面命令刷新,查看是否还有服务信息
ls /services/cloud-provider-payment
查询启动8004服务提供者,再次使用上面的命令刷新查看
2、ZooKeeper服务消费者
1、新建模块cloud-consumerzk-order80
2、修改pom.xml配置文件
与提供者一样
3、添加yaml配置文件
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 127.0.0.1:2181 # Zookeeper连接地址
4、创建启动类
package com.zcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 描述:启动类
* @EnableDiscoveryClient 该注解用于向使用consul或者zookeeper作为注册中心时注册服务
*
* @author zhong
* @date 2022-09-18 20:25
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZK80 {
public static void main(String[] args) {
SpringApplication.run(OrderZK80.class, args);
}
}
5、业务类【调用8004】
5.1、创建远程调用配置
package com.zcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* 描述:启动类
* @EnableDiscoveryClient 该注解用于向使用consul或者zookeeper作为注册中心时注册服务
*
* @author zhong
* @date 2022-09-18 20:25
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZK80 {
public static void main(String[] args) {
SpringApplication.run(OrderZK80.class, args);
}
}
5.2、控制器
package com.zcl.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* 描述:服务提供者控制器
*
* @author zhong
* @date 2022-09-18 20:29
*/
@Slf4j
@RestController
@RequestMapping("/payment")
public class OrderZKController {
/**
* 常量记录远程调用提供者地址
*/
public static final String INVOKE_URL = "http://cloud-provider-payment";
/**
* 注入远程调用
*/
@Autowired
private RestTemplate restTemplate;
/**
* 调用远程的提供者服务
* @return
*/
@RequestMapping(value = "/consumer/payment/zk")
public String paymentInfo()
{
String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
log.info("消费者调用支付服务(zookeeper)--->result:" + result);
return result;
}
}
6、启动消费者项目80
-
查看zookeeper服务端是否有注册成功
-
通过原来的消费提供者服务是否功能正常
-
通过消费者80调用是否能远程调用提供者成功
http://localhost/payment/consumer/payment/zk
更多推荐
已为社区贡献1条内容
所有评论(0)