上一篇文章中已经讲述 Feign的基本用法,本章主要概述 FeignClient
GET/POST/PUT/DELETE restful写法以及 Feign 拦截器,与配置优化方案,关闭HttpClient开启OKHTTP…
- 准备工作
1.启动Consul,所有文章都将以Consul作为服务注册中心
2.创建 battcn-feign-hello,battcn-feign-hi(本文代码基于上篇改造)
3.服务(Hi)-> FeignClient -> 服务(Hello),通过实现 RequestInterceptor 传递 header 信息
- battcn-feign-hello
- pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
- BattcnFeignHelloApplication.java
1 2 3 4 5 6 7 8
|
@SpringBootApplication @EnableDiscoveryClient public class BattcnFeignHelloApplication {
public static void main(String[] args) { SpringApplication.run(BattcnFeignHelloApplication.class, args); } }
|
- Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class Student {
private Long id; private String name; private String email;
|
- HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
@RestController @RequestMapping("/hello") public class HelloController {
@Autowired HttpServletRequest request;
static Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
@ResponseStatus(HttpStatus.OK) @GetMapping public Student findStudentByName(@RequestParam("name") String name,@RequestHeader(name = "token",required = false)) {
|
- bootstrap.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
server: port: 8765
spring: application: name: battcn-feign-hello cloud: consul: host: localhost port: 8500 enabled: true discovery: enabled: true prefer-ip-address: true
|
- 测试
访问:http://localhost:8765/hello?name=Levin
显示:{"id":1,"name":"挽歌-GET","email":"1837307557@qq.com"}
代表我们服务启动成功
- battcn-feign-hi
- pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
<dependencies> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.battcn</groupId> <artifactId>battcn-starter-swagger</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
- BattcnFeignHiApplication.java
1 2 3 4 5 6 7 8 9 10
|
@EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class BattcnFeignHiApplication {
public static void main(String[] args) { SpringApplication.run(BattcnFeignHiApplication.class, args); } }
|
- HiController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
@RestController @RequestMapping("/hi") public class HiController {
static Logger LOGGER = LoggerFactory.getLogger(HiController.class);
@Autowired HelloClient helloClient;
@ResponseStatus(HttpStatus.OK) @GetMapping public Student find(@RequestParam("name") String name,@RequestHeader(name="token",required = false)String token) {
|
- MyFeignInterceptor.java
- HelloClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
package com.battcn.client;
import com.battcn.pojo.Student; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*;
|
- bootstrap.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
server: port: 8766
spring: application: name: battcn-feign-hi cloud: consul: host: localhost port: 8500 enabled: true discovery: enabled: true prefer-ip-address: true
|
- 测试
访问:http://localhost:8766/swagger-ui.html
使用Swagger做测试
swagger测试
此处只演示GET,PUT,DELETE,POST 示例代码都包括,自行测试即可
日志:
1 2 3
|
2017-07-29 18:21:26.854 INFO 12620 --- [nio-8765-exec-2] com.battcn.controller.HelloController : [查询参数] - [Levin] 2017-07-29 18:21:26.854 INFO 12620 --- [nio-8765-exec-2] com.battcn.controller.HelloController : [Token] - [Token HA] 2017-07-29 18:21:26.854 INFO 12620 --- [nio-8765-exec-2] com.battcn.controller.HelloController : [Auth] - [My Name's request header Auth]
|
如果未实现 RequestInterceptor 那么 LOGGER.info("[Auth] - [{}]",request.getHeader("Auth"));
就无法获取到 request 中的信息
- 流程图
画图工具:https://www.processon.com/
流程图