四、服务注册与发现(Zookeeper)

  Eureka停止更新了你怎么办。https://github.com/Netflix/eureka/wiki。SpringCloud整合Zookeeper代替Eureka

一、注册中心Zookeeper

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

二、服务提供者

  新建Spring Boot Model名称:cloud-provider-payment8004

  pom.xml添加如下依赖

<dependencies>
  <!-- zookeeper服务发现 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  </dependency>
  <dependency>
    <groupId>com.jdy</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</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>
</dependencies>

  编写application.yml

server:
  port: 8004

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

  主启动类添加@EnableDiscoveryClient

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

  启动8004注册进zookeeper

  • 启动后问题,可以看到spring-cloud-starter-zookeeper-discovery中自带了zookeeper-3.5.3-beta.jar:3.5.3-beta,但是我的虚拟机上安装的zookeeper是zookeeper-3.4.10.版本冲突。

  • 解决zookeeper版本jar包冲突问题。排除zk冲突后的新POM
<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 3.4,10版本-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

  验证测试 http://localhost:8004/payment/zk

  验证测试2

 参考cloud-provider-payment8004 将cloud-consumerzk-order80注册到zookeeper并通过RestTemplate+@LoadBalanced实现服务调用。

posted @ 2021-03-18 11:23  jingdy  阅读(158)  评论(0编辑  收藏  举报