在18年7月份,Eureka2.0宣布停更了,将不再进行开发,所以对于公司技术选型来说,可能会换用其他方案做注册中心。本章学习便是使用ZooKeeper作为注册中心。

一、项目层级

 

 

 二、zk客户端 / maven依赖 / yml配置

  • zookeeper安装后的配置

    进入conf目录,复制一份zoo_sample.cfg并重命名为zoo.cfg

    打开zoo.cfg 修改dataDir=../temporary-data

  • zk启动:进入bin目录双击zkServer.cmd

  • maven依赖

    <dependencies>

        <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>

        <!--zk-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.6.3</version>
            <!--排除日志包-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
  • yml配置

  • zookeeper-provider-payment8003

# 端口
server:
  port: 8003

spring:
  application:
    name: cloud-payment-service
  cloud:
    zookeeper:
      # 集群模式用逗号隔开
      connect-string: 127.0.0.1:2181
  • zookeeper-consumer-order8004

# 端口
server:
  port: 8004

spring:
  application:
    name: cloud-order
  cloud:
    zookeeper:
      # 集群模式用逗号隔开
      connect-string: 127.0.0.1:2181

三、写简单demo并调用

  • zookeeper-provider-payment8003

    PaymentController

 1 package com.sdkj.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 import java.util.UUID;
 9 
10 /**
11  * @Author wangshuo
12  * @Date 2022/5/21, 19:14
13  * Please add a comment
14  */
15 @Controller
16 @RequestMapping(value = "/payment")
17 public class PaymentController {
18 
19     @Value("${server.port}")
20     private String serverPort;
21 
22     @RequestMapping(value = "/getZk")
23     @ResponseBody
24     public String getPaymentZk(){
25 
26         return "spring cloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID();
27     }
28 }
  • zookeeper-consumer-order8004

    AppConfig

 1 package com.sdkj.config;
 2 
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 /**
 9  * @Author wangshuo
10  * @Date 2022/5/19, 19:49
11  * Please add a comment
12  */
13 @Configuration
14 public class AppConfig {
15 
16     /**
17      * 注入restTemplate,请用请求rest接口
18      * @return
19      */
20     @Bean
21     // 标注此注解后,RestTemplate就具有了客户端负载均衡能力
22     // 负载均衡技术依赖于的是Ribbon组件~
23     // RestTemplate都塞入一个loadBalancerInterceptor 让其具备有负载均衡的能力
24     @LoadBalanced
25     public RestTemplate restTemplate(){
26         return new RestTemplate();
27     }
28 }

    OrderController

 1 package com.sdkj.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 /**
10  * @Author wangshuo
11  * @Date 2022/5/21, 19:30
12  * Please add a comment
13  */
14 @Controller
15 @RequestMapping(value = "/order")
16 public class OrderController {
17 
18     private static final String url = "http://cloud-payment-service";
19     @Autowired
20     private RestTemplate restTemplate;
21 
22     @RequestMapping(value = "/getPaymentZk")
23     @ResponseBody
24     public String getPaymentZk() {
25 
26         return restTemplate.getForObject(url + "/payment/getZk", String.class);
27     }
28 }