参考文章:Spring Cloud Feign设计原理

1.feign是spring cloud服务间相互调用的组件,声明式、模板化的HTTP客户端。类似的HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty都实现相同功能。

目录结构

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>feigtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feigtest</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<fastjson.version>1.2.32</fastjson.version> </properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--引入这个包才能使用fegin--> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> </project>

application.yml

spring:
application:
name: service-feigntest
server:
port: 8702
eureka:
client:
serviceUrl:
#指向注册中心
defaultZone: http://192.168.111.133:8888/eureka/
instance:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2

IHiService.java

package com.feigtest;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping; @FeignClient("service-hi")
public interface IHiService {
@PostMapping("/hi")
public String say();
}
FeigTestController.java
package com.feigtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class FeigTestController {
@Autowired
IHiService hiService; @RequestMapping("/hifeign")
public String getFeignService(){
return hiService.say();
}
}
FeigtestApplication
package com.feigtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeigtestApplication { public static void main(String[] args) {
SpringApplication.run(FeigtestApplication.class, args);
} }

2.feign带token,服务调用时保持token不丢失。

package com.Interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /**
* Feign配置
* 使用FeignClient进行服务间调用,传递headers信息
*/
@Configuration
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//添加token
requestTemplate.header("token", request.getHeader("token"));
}
}
												

最新文章

  1. 关于举办 2015年 Autodesk 助力云应用项目开发活动通知
  2. CentOS下SNMP的安装与使用
  3. JavaEE MyBatis
  4. 高性能网络编程(一)----accept建立连接
  5. loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination
  6. 网络文件常常提到类似&quot;./run.sh&quot;的数据,这个命令的意义是什么?
  7. Windows不重启就使环境变量修改生效
  8. linux文件系统---10
  9. jquery之on()绑定事件和off()解除绑定事件
  10. Ubuntu多系统安装注意事项
  11. 使用top工具,找出消耗CPU 较多的进程
  12. RAID,mdadm(笔记)
  13. POJ1679(次小生成树)
  14. 页面中插入百度地图(使用百度地图API)
  15. BotVS数字货币现货交易类库
  16. 将git commit的默认编辑器从nano转为vim
  17. LVS结合keepalived配置测试
  18. Python之命名空间、闭包、装饰器
  19. (贪心 优先队列)P1090合并果子 洛谷
  20. 考前停课集训 Day1 废

热门文章

  1. g50打开无线设置 fn+f7
  2. Developer Express 第三方控件使用系列方法
  3. SCAU 2015 GDCPC team_training0
  4. 本地局域网MySQL数据库连接方法(Navcat权限设置)
  5. 菜鸟程序员成长史 --记 Github 1000+ contributions
  6. linux下查看Apache的访问日志及ip
  7. openstack stein部署手册 10. horzion
  8. 08Servlet
  9. QT生成可执行的EXE程序
  10. Java初步