Spring Boot 2.2 集成 Spring Cloud Zookeeper - Feign 分布式服务消费者

1 摘要

Spring Cloud Zookeeper 分布式服务注册中心搭建可参考:

33 Spring Boot 2.2 集成 Spring Cloud Zookeeper - 分布式服务注册中心 — 2020-02-23

Spring Cloud Zookeeper 分布式服务调用-Ribbon 方式可参考:

34 Spring Boot 2.2 集成 Spring Cloud Zookeeper - Ribbon 分布式服务消费者 — 2020-02-25

本文将介绍通过 Feign 调用 Spring Cloud Zookeeper 服务.

Feign 内部集成 Ribbon,通过注解的方式,使用更加方便

2 核心 Maven 依赖

./cloud-zookeeper-feign/pom.xml
        <!-- Spring mvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring cloud zookeeper -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-all</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Spring cloud Feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

其中 ${zookeeper.version} 的版本为 3.4.12 (不要随意改版本号,会有兼容性问题)

注意: SpringBoot 的版本需要在 2.2及以上

3 配置文件

3.1 bootstrap.yml
./cloud-zookeeper-feign/src/main/resources/bootstrap.yml
## Application bootstrap config


## spring config
spring:
  cloud:
    zookeeper:
      connect-string: 172.16.140.10:2181
3.2 application.yml
./cloud-zookeeper-feign/src/main/resources/application.yml
## Application config

## Server
server:
  port: 8102

## Spring config
spring:
  application:
    name: cloud-zookeeper-feign

4 核心代码

4.1 Service 层-Feign 注解方式调用服务
./cloud-zookeeper-feign/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/feign/service/CloudZookeeperFeignService.java
package com.ljq.demo.springboot.cloud.zookeeper.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Description: Spring Cloud Zookeeper 分布式服务消费者-Feign 业务层
 * @Author: junqiang.lu
 * @Date: 2020/2/25
 */
@FeignClient(value = "cloud-zookeeper-provider")
@Service("cloudZookeeperFeignService")
public interface CloudZookeeperFeignService {


    /**
     * 打印用户名称
     *
     * @param name
     * @return
     */
    @GetMapping(value = "/api/cloud/zookeeper/hello", produces = {MediaType.APPLICATION_JSON_VALUE})
    String sayHello(@RequestParam("name") String name);


}

使用 @FeignClient(value = "cloud-zookeeper-provider") 注解即可实现对 Spring Cloud Zookeeper 服务的调用,其中 cloud-zookeeper-provide 为服务名称(serviceId,zookeeper 默认将 spring.application.name 作为服务名称)

注意:Feign 只支持一种请求方式,即如上述代码中所示,使用 @GetMapping 指定GET 方式请求,也可以使用 @PostMapping 指定 POST 请求,但是不能使用 @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}) 指定两种请求方式,否则会在启动时抛出异常,导致启动失败

4.2 Controller 层
./cloud-zookeeper-feign/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/feign/controller/CloudZookeeperFeignController.java
package com.ljq.demo.springboot.cloud.zookeeper.feign.controller;

import com.ljq.demo.springboot.cloud.zookeeper.feign.service.CloudZookeeperFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description: Spring Cloud Zookeeper 分布式服务消费者-Feign 控制层
 * @Author: junqiang.lu
 * @Date: 2020/2/25
 */
@RestController
@RequestMapping("/api/cloud/zookeeper/feign")
public class CloudZookeeperFeignController {

    @Autowired
    private CloudZookeeperFeignService cloudZookeeperFeignService;

    /**
     * 打印用户名称
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/sayHello", method = {RequestMethod.GET, RequestMethod.POST})
    public String sayHello(@RequestParam("name") String name) {
        System.out.println(new Date() + "-" + name);
        return cloudZookeeperFeignService.sayHello(name);
    }


}

4.3 SpringBoot 启动类
/Users/ljq/develop/repository/git/springBootDemo/cloud-zookeeper-feign/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/feign/CloudZookeeperFeignApplication.java
package com.ljq.demo.springboot.cloud.zookeeper.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author junqiang.lu
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class CloudZookeeperFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudZookeeperFeignApplication.class, args);
    }

}

@EnableDiscoveryClient 用于发现 Spring cloud 服务

@EnableFeignClients 用于启用 Feign 来请求 Spring Cloud Zookeeper 服务

5 测试

由于本次测试需要调用 Spring Cloud Zookeeper 服务,因此服务注册中心的项目(cloud-zookeeper-provider)必须先启动

请求接口:

GET http://127.0.0.1:8102/api/cloud/zookeeper/feign/sayHello?name=Are%20you%20%E6%AC%A7%E5%85%8B

返回参数:

Hello !Are you 欧克
server port : 8100
server timestamp: 1582615867196

从返回的结果中可以看出,返回的端口为服务注册中心项目( cloud-zoopeeper-provider)的http端口,使用 Feign 调用 Cloud Zoopeeper 服务成功

6 参考资料推荐

官方文档 Spring Cloud Zookeeper

Zookeeper 完整系列教程 Spring-Cloud-Zookeeper-Based-Demo

由springcloud ribbon的 @LoadBalanced注解的使用理解

7 Github 源码

Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo

个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.
404Code

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值