zookeeper代替eureka与springcloud整合

注册中心 zookeeper:

  • zookeeper是一个分布式协调工具,可以实现注册中心功能
  • 关闭Linux服务器防火墙后启动zookeeper服务器
  • zookeeper服务器取代Eureka服务器,zk作为服务注册中心
  •  

     

     

     

     1、支付服务注册进zookeeper

  • 1.1 创建module cloud-provider-payment8004
  •  

     1.2 引入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>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-provider-payment8004</artifactId>
    
        <dependencies>
            <!--springboot整合zookeeper客户端-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
            <!--Springboot整合web组件-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--引入自定义的api通用包,可以使用payment支付Entity-->
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </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>
    1.3 写application.yml
  • #8004表示注册到zookeeper服务器的支付服务提供者端口号
    server:
      port: 8004
    
    #服务别名----注册zookeeper到注册中心名称
    spring:
      application:
        name: cloud-provider-payment
      cloud:
        zookeeper:
          connect-string: 192.168.88.131:2181
     
    1.4主启动
  • package com.atguigu.springcloud;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient  //该注解用于想使用consul或者zookeeper作为注册中心时注册服务
    public class PaymentMain8004 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8004.class,args);
        }
    }
     
    1.5业务类
  • package com.atguigu.springcloud.controller;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.UUID;
    
    @RestController
    @Slf4j
    public class PaymentController {
        @Value("${server.port}")
        private String serverPort;
    
        @GetMapping(value = "/payment/zk")
        public String paymentzk(){
            return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
        }
    }
     
    1.6 ./zkCli.sh启动客户端,然后启动8004注册进zookeeper
  •  

     

  • 会发现启动类报错了,这是环境的问题。引入zookeeper依赖自带3.5.3 beta版,而我使用的是3.4.14,版本冲突了。
  •  

     

  •  

    解决办法:修改pom依赖,排除3.5.3版本的,并引入3.4.14版本的 

  •  

     PS: 新版本的zookeeper还会遇到slf4j和log4j包冲突的问题,此时在zookeeper去掉slf4j的依赖

  •  

     

     

     这样,微服务提供者 支付模块就入驻进zookeeper服务器

  • 1.7 验证测试
  •  

     java程序在windows系统,服务注册中心在centos系统,说明程序调试成功。

  • 继续深挖测试:

  •  

     微服务作为一个znode节点放到zookeeper服务器上,这一大串就是在zookeeper上的基本信息的json串,就能看到zookeeper上8004微服务提供者的相关信息

  •  

     zookeeper作为服务注册中心,微服务作为节点注册进来,那这个节点是临时还是持久?
    关于Eureka我们知道,偶尔有一时刻服务提供者出了点故障,Eureka会开启自我保护机制,微服务不会被删除,那现在关闭8004试一下zookeeper中的服务会有什么变化吗

  •  

     可以看出,Zookeeper也是在一定时间内,不会剔除服务,超时才剔除,所以zookeeper具备的服务节点是临时的。当你再启动8004时,zookeeper还会自动监听服务状态,但是流水号是一个新的流水号。

  • 2.订单服务注册进zookeeper

    2.1 创建module cloud-consumerzk-order80
  •  

     2.2 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>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-consumerzk-order80</artifactId>
    
        <dependencies>
            <!--springboot整合zookeeper客户端-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
                <!--排除自带的zookeeper-->
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--添加zookeeper3.4.14版本-->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.14</version>
                <exclusions>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--springboot整合web组件-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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>
     
    2.5 业务类
  •  

     

     

     

posted @ 2020-05-18 20:47  清风商  阅读(1066)  评论(0编辑  收藏  举报