弹簧启动版本1.3.8.RELEASE和云版本'Brixton.SR'相同的代码工作正常.但是'1.5.2.RELEASE'和'Dalston.RC1'不起作用.
例外
HTTP状态500 - 请求处理失败; 嵌套异常是java.lang.RuntimeException:com.netflix.client.ClientException:负载均衡器没有客户端的可用服务器:math-service
这是我的界面。
public interface SCIMServiceStub {
@RequestLine("GET /Users/{id}")
SCIMUser getUser(@Param("id") String id);
@RequestLine("GET /Groups?filter=displayName+Eq+{roleName}")
SCIMGroup isValidRole(@Param("roleName") String roleName);
}
Run Code Online (Sandbox Code Playgroud)
在这里getUser通话效果很好。但是isValidRole无法正常工作,因为最终会这样发送请求。
/Groups?filter=displayName+Eq+{roleName}"
Run Code Online (Sandbox Code Playgroud)
这里{roleName}没有解决。我在这里想念什么?感谢一些帮助,因为我对此一无所知。
编辑:1更多问题:有没有办法避免查询参数的自动url编码?
无法让Feign客户工作。首先尝试使用POST。保持与编码器/解码器有关的错误,指出类型不正确。然后在github上找到一个示例来最终调用简单的GET API,并决定尝试一下。仍然失败
在Github和在线上,我看到Feign Client Spring-Cloud,OpenFeign,Netflix.feign的多个版本具有不同的版本。谁能形容一个最好的,稳定的Feign客户应该用于生产什么?
package com.paa.controllers;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient (name="test-service",url="https://www.reddit.com/r")
public interface GetFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/java.json")
public String posts();
}
Controller:
@RestController
@RequestMapping("/some/api")
public class TestWLCController {
@Autowired
private GetFeignClient getFeignClient;
.. some stuff
@RequestMapping(value="/postSomething",method = RequestMethod.POST)
@ApiOperation(value = "Configures something",
notes = "basic rest controller for testing feign")
public ResponseEntity<SomeResponse> feignPost(
UriComponentsBuilder builder,
@ApiParam(name = "myRequest",
value = "request for configuring something",
required = true)
@Valid @RequestBody SomeRequest someRequest) { …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Boot 应用程序,我试图用它Feign来与远程服务进行通信。我@FeignClient的定义如下:
@FeignClient(name="TEST_SERVICE", url="URL")
@Component
public interface SomeServiceClient
{
@RequestMapping(
method = RequestMethod.POST,
value = "/someService",
consumes = "application/json",
produces = "application/json"
)
SomeServiceResult getServiceResult(
@RequestParam(value = "mode") String mode,
@RequestParam(value = "payload") SomeServicePayload payload
);
}
Run Code Online (Sandbox Code Playgroud)
我希望将payloadtype 的对象SomeServicePayload序列化为 JSON。我预计这会自动发生,但事实并非如此。相反,payload被序列化为其完全限定的类名。
我需要为此客户端设置自定义编码器/解码器组合吗?我该怎么做?
是否可以通过@FeignClient创建@Configuration包含模拟版本的 bean 进行测试来覆盖通过注释创建的bean?
我已经尝试过了,但似乎@FeignClientbean 是最后创建的(或者我认为是这样),因为在我的测试中,我总是注入真实版本而不是模拟版本。在同一个配置文件中,我创建了另一个没有任何注释(除了@Component)的bean,通过使用真实的名称以相同的方式模拟,并且它完美地工作。
我试过用@MockBean它来模拟它并且它有效,但该项目有一些怪癖,这使得创建另一个 Spring 上下文破坏了测试。
谢谢。
编辑。实际上,我只是调试了测试并意识到,如果我使用与 Feign 客户端相同的名称,调试器甚至不会在@Configurationbean 中停下来创建模拟版本。将名称更改为其他名称可以正常工作,但它只会使用新名称创建另一个相同类型的 bean。我在这里缺少任何配置吗?
编辑 2. 这是一个示例代码。执行这个我有那个BarService是模拟版本,但FooService它是真实的。
@FeignClient(name = "fooService")
public interface FooService {
}
@Component
public class BarService {
}
@Configuration
public class ConfigClass {
@Bean
public FooService fooService() {
return Mockito.mock(FooService.class);
}
@Bean
public BarService barService() {
return Mockito.mock(BarService.class);
}
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest
public class TestClass {
@Autowired
private FooService fooService;
@Autowired …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Sleuth 集成到我们的系统中。如果我使用带注释的接口@FeignClient,则一切正常。这些接口会自动检测,并且 Sleuth 标头会通过 REST 调用传播。
但是,我们有一些现有代码直接使用 Feign.Builder 和 Feign 注释接口(只是没有用 注释@FeignClient)。这段代码添加了一些自定义的请求拦截器、编码器、代理等。
例如:
// Feign REST interface
public interface MyService {
@RequestMapping(method = RequestMethod.GET, value = "/version")
String getVersion();
}
// Creating the builder
Feign.Builder builder = Feign.builder();
builder.requestInterceptor(new MyCustomInterceptor());
// + adding proxy, encoder, decoder, etc
// Using the builder
MyService myService = builder.target(MyService.class, "http://localhost:8080/myservice");
myService.getVersion();
Run Code Online (Sandbox Code Playgroud)
我希望这个旧代码传播 Sleuth 标头。有什么简单的方法来连接它吗?
(我想一种选择是重新设计我们的 Feign 接口以使用 @FeignClient 并重新设计所有自定义拦截器、编码器等的应用方式,但最终这可能需要大量工作并有很大风险。)
我是否需要做一个特殊的请求拦截器来手动注入这些(例如从自动装配的 Tracer)?有没有一种干净的方法(或现有的类)来做到这一点?
spring-boot spring-cloud feign spring-cloud-netflix spring-cloud-sleuth
我使用Spring Boot编写一个与HTTP rest服务器交互的应用程序.我正在连接的服务器之一(Wit.ai)使用beaerer授权令牌.产生成功响应的curl请求如下所示:
GET /message?q=sample message HTTP/1.1
Host: api.wit.ai
Authorization: Bearer XXXXXXXXXXXXX
Cache-Control: no-cache
Postman-Token: 526c3a11-8e61-4552-aa19-e913f6473753
Run Code Online (Sandbox Code Playgroud)
wit.ai文档说出关于令牌的以下内容,
Wit.ai使用OAuth2作为授权层.因此,每个API请求必须包含带有令牌的Authorize HTTP标头.访问令牌是特定于应用程序的.
我正在尝试使用@FeignClient在Spring Boot应用程序中向此端点发送GET请求.但是我端点似乎不接受我的授权令牌.这是我的FeignClient代码
@FeignClient(name="witGetter", url = "${wit.url}")
public interface WitGetter {
@RequestMapping(method = RequestMethod.GET, value = "/message?v=20180507q={text}",
headers = {"Authorization: Bearer XXXXXXXXXXXXX"})
WitResponse getWitResponse(@PathVariable("text") final String text);
}
Run Code Online (Sandbox Code Playgroud)
传递此类授权令牌的正确方法是什么?我尝试过其他一些东西,但无济于事.谢谢你的建议!
顺便说一下,下面的代码使用传统的Feign接口,但在这种情况下我需要使用@FeignClient.
public interface WitGetter {
@Headers("Authorization: Bearer XXXXXXXXXXXXX")
@RequestLine("GET /message?q={text}")
WitResponse getWitResponse(@Param("text") String text);
}
Run Code Online (Sandbox Code Playgroud)
(以下代码位于单独的配置文件中)
@Bean
public WitGetter defaultWitGetter(@Value("https://api.wit.ai") final String witUrl){
return Feign.builder().decoder(new GsonDecoder()).target(WitGetter.class, witUrl);
Run Code Online (Sandbox Code Playgroud)
}
编辑
我使用上面代码时得到的错误代码是:
线程"main"中的异常feign.FeignException:status 400读取WitGetter #getWitResponse(String,String); …
在我的应用程序中,我必须从列表中知道哪些服务器地址已启动。我找到的解决方案是从 Spring-Boot Actuator 调用每个健康端点。JSon 响应是:
{
"status": "UP"
}
Run Code Online (Sandbox Code Playgroud)
在应用程序的其他部分,我使用 Spring-Cloud 中通过@FeignClient注释定义的 Feign 客户端,效果非常好:
@FeignClient(
name = "tokenProxy",
url = "${host}:${port}"
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种配置不允许重复使用相同的客户端来调用不同地址上的相同端点。所以我必须定义我自己的自定义客户端(如果有其他解决方案,请随时告诉我!):
@GetMapping(
value = "/servers"
)
public Server discover() {
MyClient myClient = Feign.builder()
.target(
Target.EmptyTarget.create(
MyClient.class
)
);
return myClient.internalPing(URI.create("http://localhost:8090"));
}
interface MyClient {
@RequestLine("GET /actuator/health")
Server internalPing(URI baseUrl);
}
class Server {
private final String status;
@JsonCreator
public Server(@JsonProperty("status") String status) {
this.status = status;
}
public String getStatus() {
return status;
}
} …Run Code Online (Sandbox Code Playgroud) 我们最近从版本 2.0.0 升级到,在调用使用并且也具有Content-Typespring-cloud-starter-openfeign: 2.2.2 的 REST API 时遇到以下异常:consumes@RequestHeader
feign.FeignException$UnsupportedMediaType: [415] during [POST] to [http://localhost:8080/test]
[TestFeign#test(TestRequest,String)]: [{"timestamp":"2021-01-03T13:13:58.192+0000",
"status":415,"error":"Unsupported Media Type",
"message":"Invalid mime type \"application/json, application/json\":
Invalid token character ',' in token \"json, application/json\"","path":"/test"}]
Run Code Online (Sandbox Code Playgroud)
@RequestMapping(value = "/test",method = RequestMethod.POST,consumes= {"application/json"},
produces={"application/json"})
void test(@RequestBody TestRequest request,@RequestHeader("Content-Type") String contentType);
Run Code Online (Sandbox Code Playgroud)
我们进行了调查并发现,在这种情况下,两个值都按如下方式连接:
Content-Type: "application/json, application/json"
Run Code Online (Sandbox Code Playgroud)
使用时spring-cloud-starter-openfeign:2.0.0不会出现此问题。
我在 Spring Boot 中有两个 Feign 客户端做不同的事情,但希望对它们进行不同的身份验证。
@FeignClient(
name = "...",
url = "${url1}",
configuration = Config1.class
)
public interface Client1 {
@PostMapping(
path = "...",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
JsonNode doThing(@RequestBody JsonNode thing);
}
@FeignClient(
name = "...",
url = "${url2}",
configuration = Config2.class
)
public interface Client2 {
@PostMapping(
path = "...",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
JsonNode doThing(@RequestBody JsonNode thing);
}
Run Code Online (Sandbox Code Playgroud)
它们都需要基本身份验证,但用户名和密码的值不同。为此,我考虑使用单独的Config类来设置各自的客户端:
@Configuration
public class Client1 {
private final String user; …Run Code Online (Sandbox Code Playgroud) feign ×10
spring-boot ×6
java ×5
spring ×4
spring-cloud ×3
rest ×2
http ×1
hystrix ×1
jackson ×1
json ×1
mockito ×1
openfeign ×1
unit-testing ×1
wit.ai ×1