我正在尝试使用 feign.HeaderMap 注释在其余请求中传递 HTTP 标头映射,但这些标头出现在正文中。
代码如下:
@FeignClient(name = "accounts", url = "localhost:8080") 公共接口 AccountClient {
@RequestMapping(method = RequestMethod.GET, value = "/rest/accounts/get", produces = MediaType.APPLICATION_JSON_VALUE)
Account findOne(@RequestParam("id") String id, @HeaderMap Map headers);
Run Code Online (Sandbox Code Playgroud)
}
我有一个服务,它使用授权标头获取 http 请求。在处理请求时,我想使用一个 Feign Client 来查询另一个服务。对其他服务的查询应包含相同的授权标头。
目前我使用过滤器从传入请求中提取授权标头,将标头存储在 ThreadLocal 中。在构建 Feign Client 时,我使用 RequestInterceptor 从 ThreadLocal 读取授权标头并将其放入对其他服务的请求中。
这种方法并不理想,因为当我开始使用 RxJava 或 Hystrix 之类的东西时,处理请求时线程会发生变化,我必须将授权标头 ThreadLocal 从一个线程移动到另一个线程。
有什么其他选择可以解决这个问题?我正在考虑的一种方法是为每个请求创建一个新的 FeignClient,这样我就不再需要将授权存储在本地线程中。但这是个好主意吗?
我找到了项目https://github.com/swagger-api/swagger-codegen。
然而,这是生成一个基于 OpenFeign 的客户端。
有没有办法自动生成使用 Netflix 的 feign 注释和请求映射的客户端界面?
例子:
@FeignClient(name = "ldap-proxy")
public interface LdapProxyClient {
@RequestMapping(path = "/ldap-proxy/v1/users/{userNameOrEMail}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
LdapUser search(@PathVariable("userNameOrEMail") String userNameOrEMail);
}
Run Code Online (Sandbox Code Playgroud)
与以下课程相反:
谢谢
spring netflix-feign spring-cloud-feign feign swagger-codegen
我正在尝试使用动态伪装。但是从 RequestMapping 转换响应时我有很多问题。
控制器.java :
@RequestMapping("/users")
public ResponseEntity<List<User>> sendUsers
Run Code Online (Sandbox Code Playgroud)
MyFeignClient.java :
public interface MyFeignClient {
@RequestLine(value="GET /api/users")
ResponseEntity<List<User>> getUsers();}
Run Code Online (Sandbox Code Playgroud)
主类.java :
MyFeignClient callService = Feign.builder()
.encoder(new Encoder.Default())
.decoder(new Decoder.Default())
.requestInterceptor(new FeignConfig(props).getJwtRequestInterceptor())
.target(MyFeignClient.class, "http://localhost:8710");
Run Code Online (Sandbox Code Playgroud)
进而 :
ResponseEntity<List<User>> txnPool = callService.getUsers();
Run Code Online (Sandbox Code Playgroud)
但我有以下错误:
feign.codec.DecodeException User 不是此解码器支持的类型
我该如何解决?
正如Feign的文档中提到的,默认情况下,会自动重试IOException。
我们的项目中有一些另一种retry逻辑,我想阻止 Feign 的逻辑。有什么简单的方法可以禁用它吗?或者应该编写我自己的重试器?
我们正在使用 Spring cloud 的 open feign 库。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud) 文档告诉我,HTTP 503 响应被认为是可重试的,一些例外也是如此。
根据经验,我知道 feign.RetryableException 包装了 java.net.ConnectException 和其他 jnSocketException,但我看不出这种情况发生在哪里。
其他类似 java.net.SocketTimeoutException 是否被 feign.RetryableException 包装?
我正在使用 spring feign 压缩请求和响应
在服务器端:
server:
servlet:
context-path: /api/v1/
compression:
enabled: true
min-response-size: 1024
Run Code Online (Sandbox Code Playgroud)
当我从 chrome 中点击 api 时,我看到它添加了 'Accept-Encoding': "gzip, deflate, br"
在客户端:
server:
port: 8192
servlet:
context-path: /api/demo
feign.compression.response.enabled: true
feign.client.config.default.loggerLevel: HEADERS
logging.level.com.example.feigndemo.ManagementApiService: DEBUG
eureka:
client:
enabled: false
management-api:
ribbon:
listOfServers: localhost:8080
Run Code Online (Sandbox Code Playgroud)
当我看到传递的请求标头时,feign 正在传递两个标头。
Accept-Encoding: deflate
Accept-Encoding: gzip
Run Code Online (Sandbox Code Playgroud)
gradle 文件
plugins {
id 'org.springframework.boot' version '2.1.8.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor …Run Code Online (Sandbox Code Playgroud) 我有以下 FeignClient:
@FeignClient(name="FooMS",fallback=CustomerFeign.CustomerFeignImpl.class)
public interface CustomerFeign {
@RequestMapping(value="/bar/{phoneNo}")
List<Long> getFriends(@PathVariable("phoneNo") Long phoneNo);
class CustomerFeignImpl implements CustomerFeign{
@Override
public List<Long> getFriends(Long phoneNo) {
return new ArrayList<Long>(108);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当 FooMS 实例关闭时,我收到 500 错误,而不是执行回退。为什么会这样?
错误信息如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field helloAgent in com.example.client.controller.Hello required a bean of
type 'com.example.common.agent.HelloAgent' that could not be found.
Action:
Consider defining a bean of type 'com.example.common.agent.HelloAgent' in
your configuration.
Run Code Online (Sandbox Code Playgroud)
模块:test-client作为feignclient调用者。
模块:测试服务器作为feignclient接口的实现。
模块:test-common将所有feignclient放在一起。
package com.example.common.agent;
@FeignClient("hello")
public interface HelloAgent {
@GetMapping("/hello")
String hello(@RequestParam String msg);
}
Run Code Online (Sandbox Code Playgroud)
package com.example.server.controller;
@RestController
public class Hello implements HelloAgent {
@Override
public String hello(@RequestParam String msg) {
System.out.println("get " + msg);
return "Hi " + msg;
}
} …Run Code Online (Sandbox Code Playgroud) 我的情况很简单,我已经尝试了一些方法,但找不到导致此问题的原因。它声称它不能自动连接Feign客户端类,尽管这是我在Spring Boot 1.5.9中做到的。至少我很确定。尽管我在嘲笑此客户端,但在所有单元测试中一切正常。以前它是导入库的一部分,但是为了消除我可能无法正确定位Bean的可能性,我将其添加到了同一项目中。
我不是Spring或Feign方面最有经验的人,所以我想知道我在这里缺少什么。
简单的假冒客户:
@FeignClient(name = "my-other-service")
public interface OtherServiceClient {
@GetMapping(value = "/foo/{fooId}")
@ResponseBody
String getFoo(@PathVariable int fooId);
}
Run Code Online (Sandbox Code Playgroud)
主应用程序类:@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //在其他模块中的公共类MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
取决于伪装客户端的服务:
@Service
public class FooService {
private final FooRepository fooRepository;
private final BarRepository barRepository;
private OtherServiceClient otherServiceClient;
@Autowired
public OrderService(
FooRepository fooRepository,
BarRepository barRepository,
OtherServiceClient otherServiceClient) {
this.fooRepository= fooRepository;
this.barRepository = barRepository;
this.otherServiceClient = otherServiceClient;
}
Run Code Online (Sandbox Code Playgroud)
由于这可能是自动配置,因此这里是配置报告:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches: …Run Code Online (Sandbox Code Playgroud)