SpringCloud-2.0-周阳(6. 服务注册发现 – ZooKeeper)学习笔记

  • Post author:
  • Post category:其他



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


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



1. 准备工作

  • 在 Linux 上部署 Zookeeper

    我找的教程,也可以自行寻找


    https://www.cnblogs.com/zhiyouwu/p/11546097.html

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

    systemctl stop firewalld
    
  • 开启 Zookeeper

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

    ifconfig
    

    在这里插入图片描述

  • ping 一下 Windows 查看是否连通

    在这里插入图片描述

    在这里插入图片描述

  • 最后打开 Zookeeper 客户端

    ./zkCli.sh
    



2. 服务提供者 Provider

  1. 新建一个模块 :

    cloud-provider-payment-8004

  2. 修改 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>
    
  3. 编写 YML

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

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

  • 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();
        }
    }
    
  1. 启动 Zookeeper

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  2. 启动 8004

    在这里插入图片描述

    在这里插入图片描述

  3. 解决办法

    	<!-- 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>
    
  4. 重启 8004,没有报错成功启动

  5. 去 Zookeeper 上查看,是否注册进去

    在这里插入图片描述

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

    在这里插入图片描述



3. 服务消费者 Consumer

  1. 新建一个模块 :

    cloud-consumer-order-ZK-80

  2. 修改 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>
    
  3. 编写 YML

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

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

  • 配置 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;
        }
    }
    
  1. 启动测试

    Zookeeper

    在这里插入图片描述

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

    在这里插入图片描述



版权声明:本文为weixin_44449838原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。