通过OpenFeign远程调用同局域网的其他接口
需要的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- feign httpclient -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
项目结构
关于Client的编写
@Repository
@FeignClient(contextId = "PayClient", value = "ruoyi-pay",url = "192.168.31.47:9206")
public interface PayClient {
@GetMapping("/pay/chargeable/list?pageNum=1&pageSize=10")
TableDataInfo list(@RequestBody PayChargeable payChargeable);
}
拦截器的编写
@Component
public class FeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(requestAttributes != null){
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String authorization = request.getHeader("Access-Control-Allow-Origin");
requestTemplate.header("Access-Control-Allow-Origin", "*");
}
}
}
作用: 将token放入请求头中
编写Controller
先将Client自动注入
标签:调用,OpenFeign,requestAttributes,接口,局域网,public From: https://www.cnblogs.com/msy-2000/p/17321153.html