介绍 Spring Cloud Zookeeper

介绍 Spring Cloud Zookeeper

本文我们介绍Zookeeper如何用作服务发现组件。利用自动配置及绑定Spring环境,Spring Cloud Zookeeper提供了Spring Boot应用对Apache Zookeeper的整合。

1. 需求说明

为了演示,需要创建两个服务:

  • 服务提供者应用
  • 服务消费者应用

Apache Zookeeper作为服务发现的协调者,安装Apache Zookeeper请参考其官网。

2. 注册服务提供者

启用服务注册需要增加spring-cloud-starter-zookeeper-discovery 依赖,然后在主应用上使用@EnableDiscoveryClient注解。下面一步一步展示如何搭建服务提供者,让其通过Get请求返回 “Hello World!”。

2.1 maven依赖

首先需要在pom.xml文件中增加必要的依赖: spring-cloud-starter-zookeeper-discovery, spring-web, spring-cloud-dependencies 和 spring-boot-starter。

<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>

2.2 服务提供注解

接下来在主类上增加@EnableDiscoveryClient注解,使得HelloWorld应用能够感知服务发现:

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

现在添加一个简单controller:

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

2.3 yaml配置

最后创建YAML Application.yml 配置文件,配置应用的日志级别并通知Zookeeper应用启用服务发现。配置应用名称非常重要,后面服务消费者feign客户端通过该名称查询服务:

spring:
  application:
    name: HelloWorld
server:
  port: 8081
endpoints:
  restart:
    enabled: true
logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

Spring boot应用缺省在2181端口查找zookeeper,当然需要配置zookeeper位置,安装在本地时增加下面内容:

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

3. 注册服务消费者

现在创建Rest服务消费者,并注册Spring Netflix Fveign Client。

3.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>
 

3.2 服务消费注解

与服务提供者一样,在主类上增加@EnableDiscoveryClient 注解使得感知服务发现:

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

3.3 使用Feign Client发现服务

我们使用Spring Cloud Feign Integration————Netflix提供的声明式REST客户端,仅需声明服务url,feign复杂连接rest服务。Feign Client时通过spring-cloud-starter-feign 包导入。因此在应用中增加@Configuration 和 @EnableFeignClients注解。

最后在接口上增加@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();
    }
}

3.4 定义Controller

下面是简单服务Controller 类,通过feign客户端调用服务提供者消费服务(当然是通过服务发现进行调用)。

@RestController
public class GreetingController {
 
    @Autowired
    private HelloWorldClient helloWorldClient;
 
    @GetMapping("/get-greeting")
    public String greeting() {
        return helloWorldClient.helloWorld();
    }
}

3.5 YAML 配置

创建YAML文件,与前面类似配置应用日志级别:

spring:
  application:
    name: Greeting
  cloud:
    zookeeper:
      connect-string: localhost:2181
server:
  port: 8083
logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

配置zookeeper:

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181
      discovery:
        enabled: true

4. 测试应用

在部署时HelloWorld Rest服务注册自身至Zookeeper中,然后Greeting服务作为服务消费者使用Feign客户端调用HelloWorld服务。现在构建并启动两个服务,最后通过 http://localhost:8083/get-greeting 进行访问,输出响应结果:

Hello World!

5. 总结

本文介绍了使用Spring Cloud Zookeeper作为服务发现,然后在zookeeper中注册服务提供者和服务消费者,最后消费者通过Feign客户端进行服务调用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值