SpringCloud-2.0(6. 服务注册发现 - ZooKeeper)

Posted ABin-阿斌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud-2.0(6. 服务注册发现 - ZooKeeper)相关的知识,希望对你有一定的参考价值。

我是 ABin-阿斌:写一生代码,创一世佳话,筑一揽芳华。 如果小伙伴们觉得我的文章有点 feel ,那就点个赞再走哦。
在这里插入图片描述

上一篇 :5. 服务注册发现 - Eureka

下一篇 :7. 服务注册发现 - Consul

1 . 准备工作

  • 在 Linux 上部署 Zookeeper

  • 如果 Zookeeper 部署在 Linux 上,先关闭防火墙

    systemctl stop firewalld
    
    
  • 开启 Zookeeper

    ./zkServer.sh start
    
    
  • 开启 Zookeeper 后,查看 ip

    ifconfig
    
    

  • ping 一下 Windows 查看是否连通

  • 最后打开 Zookeeper 客户端

    ./zkCli.sh
    
    

2 . 服务提供者 Provider

  • 新建一个模块 :cloud-provider-payment-8004

  • 修改 POM 文件

    <dependencies>
        <dependency>
            <groupId>com.demo.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    
  • 编写 YML

    server:
      port: 8004
    
    spring:
      application:
        name: cloud-provider-payment
      # 这里和之前不同
      cloud:
        zookeeper:
          # 这里要换成自己 Liunx 系统的 IP
          connect-string: 192.168.142.120:2181
    
    
  • 编写主启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ZookeeperMain8004 {
        public static void main(String[] args) {
            SpringApplication.run(ZookeeperMain8004.class, args);
        }
    }
    
    
  • 编写业务类

  • Controller

    @Slf4j
    @RestController
    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();
        }
    }
    
    
  • 启动 Zookeeper

  • 启动 8004

  • 解决办法

    	<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--排除zk3.5.3-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加zk 对应的版本,按照自己的 Zookeeper 版本来-->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.11</version>
        </dependency>
    
    
  • 重启 8004,没有报错成功启动

  • 去 Zookeeper 上查看,是否注册进去

    访问 :http://localhost:8004/payment/zk

3 . 服务消费者 Consumer

  • 新建一个模块 :cloud-consumer-order-ZK-80

  • 修改 POM 文件

    <dependencies>
        <dependency>
            <groupId>com.demo.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!--排除zk3.5.3-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加zk 对应的版本,按照自己的 Zookeeper 版本来-->
        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.11</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    
  • 编写 YML

    server:
      port: 80
    
    spring:
      application:
        name: cloud-consumer-order
      cloud:
        zookeeper:
          connect-string: 192.168.142.120:2181
    
    
  • 编写主启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    public class OrderZKMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderZKMain80.class, args);
        }
    }
    
    
  • 编写业务类

  • 配置 RestTemplate 的 Bean

    @Configuration
    public class ApplicationContextConfig {
        @Bean
        @LoadBalanced
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }
    
    
  • 编写 Controller

    @Slf4j
    @RestController
    public class OrderZKController {
        public static final String INVOME_URL = "http://cloud-provider-payment";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/zk")
        public String payment (){
            String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class);
            return result;
        }
    }
    
    
  • 启动测试

    Zookeeper

    访问 http://localhost/consumer/payment/zk

以上是关于SpringCloud-2.0(6. 服务注册发现 - ZooKeeper)的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud-2.0(5. 服务注册发现 - Eureka)

SpringCloud-2.0(4. Rest工程构建)

SpringCloud-2.0-周阳(17. SpringCloud Alibaba入门简介)

SpringCloud-2.0(8. 负载均衡 - Ribbon)

SpringCloud-2.0:(2. Cloud相关组件介绍)

SpringCloud-2.0-周阳:(12. 服务网关 - Gateway)