SpringCloud------zookeeper代替Eureka(七)
SpringCloud整合zookeeper代替Eureka
注册中心zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后,启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为注册中心
首先对已经安装zookeeper的Linux系统防火墙进行关闭
systemctl stop firewalld # 关闭防火墙命令
systemctl status firewalld # 查看防火墙状态
打印输出中显示
Active:inactive
说明显示防火墙已经关闭。
另外需要保证zookeeper所在服务器IP与本机能够ping通
新建8004作为服务提供者注册进zookeeper
新建module的步骤就不再重复
主要是对pom文件以及yml文件的修改
其他jar包一致,没有什么变化
pom依赖替换
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
替换成
<!-- SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 排除与zookeeper版本不一致导致 冲突的 jar包 -->
<!-- org.springframework.cloud:spring-cloud-starter-zookeeper-discovery:2.2.0.RELEASE中会自带zookeeper,可能会产生版本不一致 -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加对应版本的jar包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
<!-- 排除和 slf4j 冲突的 jar包 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
修改yml文件,新增zookeeper服务的地址
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
connect-string: 192.168.16.104:2181 # zk地址 192.168.16.104是linux地址
主启动方法
@EnableDiscoveryClient
需要与服务器打交道,查上面的的信息,基本上都是用EnableDiscoveryClient这个注解,尤其是consul以及zookeeper
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class,args);
}
}
Controller:
@RestController
@Slf4j
@RequestMapping("/paymentController")
public class PaymentController {
@Value("${server.port}")
private String serverPort; // 8004
@RequestMapping("/zk")
public String paymentzk() {
return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
}
以上就是微服务注册进zookeeper的全部过程
启动zookeeper
./zkCli.sh
查看zookeeper中的服务
ls /
springframework.cloud中自带zookeeper,与使用的版本不一致引起冲突。
因此需要在导入时,将自带的zookeeper依赖排除
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 排除与zookeeper版本不一致导致 冲突的 jar包 -->
<!-- org.springframework.cloud:spring-cloud-starter-zookeeper-discovery:2.2.0.RELEASE中会自带zookeeper,可能会产生版本不一致 -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
查看服务信息json串
通过ls 获取id
根据get id 获取对应服务的具体信息
zookeeper节点划分
zookeeper存在4种节点:临时节点、带序号的临时节点、持久节点、带序号的持久节点。
此时的服务提供者是什么性质的节点?
经过将服务提供者停止,后再启动,发现id变化,不再是同一个,由此得出服务提供者在zookeeper中是临时节点
新建8004作为服务提供者注册进zookeeper
pom文件与服务提供者一致。
yml文件中的端口号、以及服务名进行修改即可。
# 开发环境配置
server:
# 服务器的HTTP端口,默认为8080
port: 8082
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.16.104:2181 # zk地址 192.168.16.104是linux
主启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZk80 {
public static void main(String[] args) {
SpringApplication.run(OrderZk80.class,args);
}
}
Controller:业务类
由于还没有使用Ribbon,因此还是使用RestTemplate形式进行调用
/**
* RestTemplate 配置类类
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
业务代码:
@RestController
@RequestMapping("/consumer")
public class OrderZkController {
@Autowired
private RestTemplate restTemplate;
public static final String URL = "http://cloud-payment-service";
/**
* 通过调用RestTemplate,调用生产者接口
*/
@GetMapping("/getZk")
public String paymentInfo(){
return restTemplate.getForObject(URL+"/paymentController/zk",String.class);
}
}
至于zookeeper的集群版暂无讲解
注册进就是这样,至于详细操作再具体学习
connect-string: 192.168.16.104:2181,192.168.16.105:2181
最后附完整pom文件
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.springcloud2022</groupId>
<artifactId>SpingCloudDemo1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<!-- <version></version>-->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.springcloud2022</groupId>
<artifactId>cloud-api_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 排除与zookeeper版本不一致导致 冲突的 jar包 -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加对应版本的jar包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
<!-- 排除和 slf4j 冲突的 jar包 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>