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