An Intro to Spring Cloud Zookeeper – Spring Cloud Zookeeper的介绍

最后修改: 2017年 4月 25日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.简介

In this article, we will get acquainted with Zookeeper and how it’s used for Service Discovery which is used as a centralized knowledge about services in the cloud.

在这篇文章中,我们将熟悉Zookeeper以及它是如何被用于服务发现的,它被用作云中服务的集中知识。

Spring Cloud Zookeeper provides Apache Zookeeper integration for Spring Boot apps through autoconfiguration and binding to the Spring Environment.

Spring Cloud Zookeeper通过自动配置和与Spring环境的绑定为Spring Boot应用程序提供Apache Zookeeper集成。

2. Service Discovery Setup

2.服务发现设置

We will create two apps:

我们将创建两个应用程序。

  • An app that will provide a service (referred to in this article as the Service Provider)
  • An app that will consume this service (called the Service Consumer)

Apache Zookeeper will act as a coordinator in our service discovery setup. Apache Zookeeper installation instructions are available at the following link.

Apache Zookeeper将在我们的服务发现设置中充当协调者。Apache Zookeeper的安装说明可在以下链接获得。

3. Service Provider Registration

3.服务提供者注册

We will enable service registration by adding the spring-cloud-starter-zookeeper-discovery dependency and using the annotation @EnableDiscoveryClient in the main application.

我们将通过添加spring-cloud-starter-zookeeper-discovery依赖关系并在主应用程序中使用注释@EnableDiscoveryClient来启用服务注册。

Below, we will show this process step-by-step for the service that returns “Hello World!” in a response to GET requests.

下面,我们将逐步展示这个过程,该服务在响应GET请求时返回 “Hello World!”。

3.1. Maven Dependencies

3.1.Maven的依赖性

First, let’s add the required spring-cloud-starter-zookeeper-discovery, spring-web, spring-cloud-dependencies and spring-boot-starter dependencies to our pom.xml file:

首先,让我们在我们的spring-cloud-starter-zookeeper-discoveryspring-webspring-cloud-dependenciesspring-boot-starterdependencies添加所需pom.xml文件。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<version>2.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
        <version>5.1.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
     </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.2. Service Provider Annotations

3.2.服务提供者注解

Next, we will annotate our main class with @EnableDiscoveryClient. This will make the HelloWorld application discovery-aware:

接下来,我们将用@EnableDiscoveryClient来注释我们的主类。这将使HelloWorld应用程序具有发现意识。

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

And a simple controller:

还有一个简单的控制器。

@GetMapping("/helloworld")
public String helloWorld() {
    return "Hello World!";
}

3.3. YAML Configurations

3.3.YAML配置

Now let us create a YAML Application.yml file that will be used for configuring the application log level and informing Zookeeper that the application is discovery-enabled.

现在让我们创建一个YAML Application.yml文件,该文件将用于配置应用程序的日志级别,并通知Zookeeper该应用程序已启用发现功能。

The name of the application with which gets registered to Zookeeper is the most important. Later in the service consumer, a feign client will use this name during the service discovery:

被注册到Zookeeper的应用程序的名称是最重要的。在以后的服务消费者中,feign客户端将在服务发现中使用这个名字。

spring:
  application:
    name: HelloWorld
  cloud:
    zookeeper:
      discovery:
        enabled: true
logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

The spring boot application looks for zookeeper on default port 2181. If zookeeper is located somewhere else, the configuration needs to be added:

spring boot应用程序在默认的2181端口寻找zookeeper。如果zookeeper位于其他地方,需要添加配置。

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

4. Service Consumer

4.服务消费者

Now we will create a REST service consumer and registered it using Spring Netflix Feign Client.

现在,我们将创建一个REST服务消费者,并使用Spring Netflix Feign Client注册它。

4.1. Maven Dependency

4.1.Maven的依赖性

First, let’s add the required spring-cloud-starter-zookeeper-discovery, spring-web, spring-cloud-dependencies, spring-boot-starter-actuator and spring-cloud-starter-feign dependencies to our pom.xml file:

首先,让我们添加所需的spring-cloud-starter-zookeeper-discovery, spring-web, spring-cloud-dependenciesspring-boot-starter-actuatorspring-cloud-starter-feign依赖项到我们的pom.xml文件。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4.2. Service Consumer Annotations

4.2.服务消费者注解

As with the service provider, we will annotate the main class with @EnableDiscoveryClient to make it discovery-aware:

与服务提供者一样,我们将用@EnableDiscoveryClient来注解主类,使其具有发现意识。

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

4.3. Discover Service With Feign Client

4.3.用伪装的客户端发现服务

We will use the Spring Cloud Feign Integration, a project by Netflix that lets you define a declarative REST Client. We declare how the URL looks like and feign takes care of connecting to the REST service.

我们将使用Spring Cloud Feign Integration,Netflix的一个项目,让你定义一个声明式的REST客户端。我们声明URL的样子,feign负责连接到REST服务。

The Feign Client is imported via the spring-cloud-starter-feign package. We will annotate a @Configuration with @EnableFeignClients to make use of it within the application.

Feign Client是通过spring-cloud-starter-feign包导入。我们将用@Configuration来注释@EnableFeignClients,以便在应用程序中使用它。

Finally, we annotate an interface with @FeignClient(“service-name”) and auto-wire it into our application for us to access this service programmatically.

最后,我们用@FeignClient(“service-name”)注释了一个接口,并将其自动连接到我们的应用程序中,以便我们以编程方式访问该服务。

Here in the annotation @FeignClient(name = “HelloWorld”), we refer to the service-name of the service producer we previously created.

在这里的注解@FeignClient(name = “HelloWorld”)中,我们引用了我们之前创建的服务生产者的service-name

@Configuration
@EnableFeignClients
@EnableDiscoveryClient
public class HelloWorldClient {
 
    @Autowired
    private TheClient theClient;

    @FeignClient(name = "HelloWorld")
    interface TheClient {
 
        @RequestMapping(path = "/helloworld", method = RequestMethod.GET)
        @ResponseBody
	String helloWorld();
    }
    public String HelloWorld() {
        return theClient.HelloWorld();
    }
}

4.4. Controller Class

4.4.控制器类

The following is the simple service controller class that will call the service provider function on our feign client class to consume the service (whose details are abstracted through service discovery) via the injected interface helloWorldClient object and displays it in response:

下面是简单的服务控制器类,它将通过注入的接口helloWorldClient对象调用我们的feign客户类上的服务提供者函数来消费服务(其细节通过服务发现来抽象),并在响应中显示它。

@RestController
public class GreetingController {
 
    @Autowired
    private HelloWorldClient helloWorldClient;

    @GetMapping("/get-greeting")
    public String greeting() {
        return helloWorldClient.helloWorld();
    }
}

4.5. YAML Configurations

4.5.YAML配置

Next, we create a YAML file Application.yml very similar to the one used before. That configures the application’s log level:

接下来,我们创建一个YAML文件Application.yml,与之前使用的文件非常相似。该文件配置了应用程序的日志级别。

logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

The application looks for the Zookeeper on default port 2181. If Zookeeper is located somewhere else, the configuration needs to be added:

该应用程序在默认端口2181上寻找Zookeeper。如果Zookeeper位于其他地方,需要添加配置。

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

5. Testing the Setup

5.测试设置

The HelloWorld REST service registers itself with Zookeeper on deployment. Then the Greeting service acting as the service consumer calls the HelloWorld service using the Feign client.

HelloWorld REST服务在部署时向Zookeeper注册了自己。然后作为服务消费者的Greeting服务使用Feign客户端调用HelloWorld服务。

Now we can build and run these two services.

现在我们可以建立并运行这两个服务。

Finally, we’ll point our browser to http://localhost:8083/get-greeting, and it should display:

最后,我们将浏览器指向http://localhost:8083/get-greeting,它应该显示。

Hello World!

6. Conclusion

6.结论

In this article, we have seen how to implement service discovery using Spring Cloud Zookeeper and we registered a service called HelloWorld within Zookeeper server to be discovered and consumed by the Greeting service using a Feign Client without knowing its location details.

在这篇文章中,我们看到了如何使用Spring Cloud Zookeeper实现服务发现,我们在Zookeeper服务器中注册了一个名为HelloWorld的服务,以便在不知道其位置细节的情况下,使用Feign Client发现并消费Greeting服务。

As always, the code for this article is available on the GitHub.

一如既往,本文的代码可在GitHub上获得。