SpringCloud2020学习笔记05——Zookeeper服务注册与发现

目录

  • 一、Eureka停止更新
  • 二、SpringCloud整合Zookeeper代替Eureka
    • 1、注册中心zookeeper
    • 2、服务提供者
      • ① cloud-provider-payment8004
      • ② POM
      • ③YML
      • ④ 主启动类
      • ⑤ Controller
      • ⑥ 排除zookeeper冲突
      • ⑦ 云服务器需要配置安全规则
      • ⑧ 测试
      • ⑨ 测试2
    • 3、服务消费者
      • ① cloud-consumerzk-order80
      • ② POM
      • ③ YML
      • ④ 主启动
      • ⑤ Controller
      • ⑥ 配置Bean
      • ⑦ 测试

一、Eureka停止更新

https://github.com/Netflix/eureka/wiki
SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第1张图片

二、SpringCloud整合Zookeeper代替Eureka

1、注册中心zookeeper

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

2、服务提供者

① cloud-provider-payment8004

② POM

<dependencies>

        <dependency>
            <groupId>com.radish.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>


        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


    dependencies>

③YML

server:
  port: 8004

spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: x.x.x.x:2181

④ 主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class,args);
    }
}

⑤ Controller

@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();
    }
}

⑥ 排除zookeeper冲突

因为服务器上zookeeper的版本为3.4.9


        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeepergroupId>
                    <artifactId>zookeeperartifactId>
                exclusion>
            exclusions>
        dependency>
            
        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.9version>
        dependency>

⑦ 云服务器需要配置安全规则

SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第2张图片

⑧ 测试

我是用docker下载开启的,所以要先要用docker进入zookeeper容器docker exec -it zk01 bash
在这里插入图片描述
执行 ./bin/zkCli.sh 启动客户端,连接zookeeper
SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第3张图片
输入 ls /services 查看
在这里插入图片描述
测试成功!

⑨ 测试2

SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第4张图片

SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第5张图片
SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第6张图片

3、服务消费者

① cloud-consumerzk-order80

② POM

 <dependencies>

        <dependency>
            <groupId>com.radish.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>


        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeepergroupId>
                    <artifactId>zookeeperartifactId>
                exclusion>
            exclusions>
        dependency>
        
        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.9version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


    dependencies>

③ YML

同服务提供者

④ 主启动

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: 47.106.255.18:2181

⑤ Controller

@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();
    }
}

⑥ 配置Bean

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

⑦ 测试

SpringCloud2020学习笔记05——Zookeeper服务注册与发现_第7张图片

你可能感兴趣的:(SpringCloud,学习笔记,学习)