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
,通过注解的方式,使用更加方便
./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及以上
./cloud-zookeeper-feign/src/main/resources/bootstrap.yml
## Application bootstrap config
## spring config
spring:
cloud:
zookeeper:
connect-string: 172.16.140.10:2181
./cloud-zookeeper-feign/src/main/resources/application.yml
## Application config
## Server
server:
port: 8102
## Spring config
spring:
application:
name: cloud-zookeeper-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})
指定两种请求方式,否则会在启动时抛出异常,导致启动失败
./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);
}
}
/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 服务
由于本次测试需要调用 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
服务成功
Zookeeper 完整系列教程 Spring-Cloud-Zookeeper-Based-Demo
由springcloud ribbon的 @LoadBalanced注解的使用理解
commit 9c7ce8ffc57f4ceb1d510914c66e239476b4e678 (HEAD -> dev, origin/master, origin/dev, origin/HEAD, master)
Author: ljq <flying9001@gmail.com>
Date: Tue Feb 25 15:34:08 2020 +0800
代码-添加 SpringBoot 2.2 集成 Spring Cloud Zookeeper - Feign 分布式服务调用
版本回退命令
git reset --soft 9c7ce8ffc57f4ceb1d510914c66e239476b4e678