SpringCloud integrates Zookeeper to implement service registration center

Reprinted Disclaimer: The source of the article is to carry sacks of teenager


Write at the beginning

  Continued from the previous article: Spring Cloud integrates Eureka to implement a service registry . With the closed source of Eureka 2.0 stop. As the preferred service registration and discovery component of Spring Cloud, Eureka's status as a prince is not guaranteed.

  We can learn about Eureka through the Eureka GitHub wiki page . There is such a content in the text: Eureka 1.x is a core part of Netflix's service discovery system and is still an active project.. Although Eureka 2.0 has been discontinued, version 1.0 is still active. Therefore, for some enterprises, Eureka functions can be used, and more content will not be used in general development. So whether you want to abandon Eureka and find new love in the enterprise depends on the actual situation.

  大多数企业出于成本考虑,都会拿 ZooKeeper 来替代 Eureka. At this point, if your project uses the Dubbo + Zookeeper solution and you want to replace it with Spring Cloud, then Zookeeper as the service registry of Spring Cloud can't be more familiar.(因为Dubbo推荐使用 Zookeeper 作为服务注册中心)

  Zookeeper can be used as the service registration center recommended by Dubbo, so Zookeeper can completely replace Eureka to realize service registration and discovery.(不过实际开发中,使用 Zookeeper 替代 Eureka 还是不多。因为还有更好的 Nacos)

Zookeeper service registry architecture

Insert picture description here

Ready to work

1. Zookeeper preparation

  Now that you want to use Zookeeper as the service registry of Spring Cloud, you must first install a Zookeeper. For installation steps, please refer to: ZooKeeper cluster installation . You don't need a Zookeeper cluster, just a Zookeeper instance.Zookeeper单实例安装很简单,下载 tar.gz 解压缩,进入 conf 目录,将 zoo_simple.cfg 更名为 zoo.cfg ,进入 bin 目录 通过 sh zkServer.sh start 即可启动。是否启动成功,可通过 sh zkServer.sh status 查看状态

  In this article, Zookeeper is installed in Linux environment, IP is 192.168.204.201 (virtual machine environment), version: 3.4.14. If you do not know to install a virtual machine, skip link: VMware virtual machine and how to properly install networking understanding

2. Operate on the service provider

  The module name is defined as:, cloud-provider-payment8004to act as a service provider.

2.1 pom.xml introduces dependencies

  The introduction of spring-cloud-starter-zookeeper-discoverydependency. Remember to check the zookeeper version that comes with the spring cloud integration zookeeper dependency package. If there is a conflict with the zookeeper client version we installed, we need to manually exclude it (exclusion)

<!--父pom.xml就是一些基础依赖,请下载工程代码查看-->
<!--引入spring cloud zookeeper 依赖-->
<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.14版本-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>
</dependency>

 
  
  
2.2 Application.yml configuration file modification
#8004 表示注册到 zookeeper 服务器的端口号
server:
  port: 8004
#服务别名---注册zookeeper到注册中心的名称
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      # zk地址,此处是集群的话,使用逗号隔开
      connect-string: 192.168.204.201:2181

 
  
  
2.3 Write the 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();
  }
}

 
  
  
2.4 Project start

  When starting, if the following error is reported, take a look at the error about the logback log package, so here is simply to exclude the logback package used by spring boot by default and replace it with log4j. (Spring boot integrates logback by default, performance is better than log4j)

  I directly excluded logback here and replaced it with log4j. If you need to use logback, then look at how to solve this one. 到此为止,服务启动模块启动OK. The pom.xml configuration is as follows:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 使用log4j2 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

 
  
  
2.5 Check whether the zk service is successfully registered

  By .zkCli.shway into zk client, a command ls /can see a services folder into the folder will be able to see us 服务提供模块的应用名. Service provision module, registration zk is successful.
Insert picture description here

3. Operate on the service consumer

  The module name is defined as:, cloud-consumerzk-order80to act as a service consumer.

3.1 pom.xml introduces dependencies

  With 服务提供模块the same. All problems with the zk version that need to be eliminated are eliminated, and logback is also eliminated with reference to 2.4.

3.2 Application.yml configuration file modification
server:
  port: 80
spring:
  application:
    name: cloud-consumer-order
  cloud:
    zookeeper:
      connect-string: 192.168.204.201:2181

 
  
  
3.3 Also use RestTemplate for service calls
@Configuration
public class ApplicationContextConfig {
    
    
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
}

 
  
  

Insert picture description here

3.4 Write the controller
@RestController
@Slf4j
public class OrderZKController {
    
    
	//使用服务名的方式调用
    public static final String INVOME_URL = "http://cloud-provider-payment";
    @Resource
    private RestTemplate restTemplate;
    @GetMapping("/consumer/payment/zk")
    public String payment (){
    
    
        String result = restTemplate.getForObject(INVOME_URL+"/payment/zk",String.class);
        return result;
    }
}

 
  
  
3.4 Check whether the zk service is successfully registered

Insert picture description here

3.5 Interface call test

Insert picture description here

The code download address of this article: SpringCloud integrates Zookeeper to implement service registry (extract code: tqns)
 
Next article: SpringCloud integrates Consul to implement service registry

Guess you like

Origin blog.csdn.net/m0_37989980/article/details/108460811