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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章