• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
思想人生从关注生活开始
博客园    首页    新随笔    联系   管理    订阅  订阅

Spring Cloud Zookeeper 简介

一、简介

 

在本文中,我们将了解 Zookeeper 以及它如何用于服务发现,它被用作云中服务的集中知识。

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

2.服务发现设置

 

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

  • 将提供服务的应用程序(在本文中称为Service Provider )
  • 将使用此服务的应用程序(称为Service Consumer)

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

3. 服务提供者注册

 

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

 

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

3.1。Maven 依赖项

 

首先,让我们将所需的spring-cloud-starter-zookeeper-discovery、spring-web、spring-cloud-dependencies和spring-boot-starter依赖项添加到我们的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. 服务提供者注解

 

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

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

还有一个简单的控制器:

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

3.3. YAML 配置

 

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

 

注册到 Zookeeper 的应用程序的名称是最重要的。稍后在服务消费者中,feign客户端将在服务发现期间使用此名称:

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

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

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

4.服务消费者

 

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

4.1。Maven 依赖

 

首先,让我们将所需的spring-cloud-starter-zookeeper-discovery、spring-web、spring-cloud-dependencies、spring-boot-starter-actuator和spring-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. 服务消费者注解

 

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

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

4.3. 使用 Feign 客户端发现服务

 

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

Feign客户端通过spring-cloud-starter- feign包导入。我们将使用@EnableFeignClients注释@Configuration以在应用程序中使用它。

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

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

@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. 控制器类

 

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

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

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

4.5. YAML 配置

 

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

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

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

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

5. 测试设置

 

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

 

现在我们可以构建和运行这两个服务。

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

Hello World!
posted @ 2022-05-10 11:11  JackYang  阅读(941)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3