我正在使用 Spring Cloud Kubernetes,我试图让 feign 能够根据 kubernetes 中存在的服务的名称发送请求,但是我不能,当我尝试发出请求时,会发生以下错误:
"timestamp": "2019-12-06T15:37:50.285+0000",
"status": 500,
"error": "Internal Server Error",
"message": "com.netflix.client.ClientException: Load balancer does not have available server for client: poc-saldo",
"trace": "java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: poc-saldo\n\tat org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute....
Run Code Online (Sandbox Code Playgroud)
我尝试调用集群内的其他服务,但所有这些服务的问题都是一样的,我通过进入 poc-deposit pod 并执行 poc-balance curl 进行了测试,它正常工作,所以问题不在于poc 存款服务。平衡或显然与 kubernetes 的服务发现。
该项目的公开资料位于:
https://gitlab.com/viniciusxyz/spring-kubernetes-feign
Run Code Online (Sandbox Code Playgroud)
对于那些想要更直接信息的人:
我的主要课程如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceDiscoveryApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我与feign的界面如下:
@FeignClient("poc-saldo")
public interface ProxyGenerico {
@RequestMapping(method …Run Code Online (Sandbox Code Playgroud) 我目前只是在检查如何在我的一个项目中使用 Feign 作为声明性 REST 客户端。
以下是 Feign 客户端
@FeignClient(name = "SEARCHCABMS",configuration = AppFeignConfiguration.class)
public interface SearchCabMsClient {
@RequestMapping(value = "api/searchcab/locationcabtimedetail/search/getCabForLocationAfterTimeSlot", method = RequestMethod.GET)
String searchCabDetails(@PathVariable("fromDate") String fromDate,
@PathVariable("locationId") long locationId,
@PathVariable("isdeleted") byte isdeleted,
@PathVariable("hourforbooking")int hourforbooking);
}
Run Code Online (Sandbox Code Playgroud)
此接口在一项服务中自动装配
@Autowired
SearchCabMsClient restService;
Run Code Online (Sandbox Code Playgroud)
将 EnableFeignClients 添加到 SpringBootApplication
@EnableFeignClients(basePackages = {"com.gp.cabbooking.services.feign"})
Run Code Online (Sandbox Code Playgroud)
pom.xml 中的依赖项、父项等
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在为我的假客户端调试 spring 创建代理时,我可以看到我尝试在功能区的帮助下调用此服务,它工作正常,但是当我在我的假客户端中执行方法定义时。我得到了feign.FeignException: status 404 reading
feign.FeignException: status …Run Code Online (Sandbox Code Playgroud) 我知道Feign是声明性的,因此它为开发人员抽象了很多东西.但是,何时应该选择一个而不是另一个?虽然假装是声明性的,但它与oAuth存在严重问题.使用RestTemplate而不是Feign有哪些注意事项
我有一个宁静的服务,使用Spring Cloud Feign客户端调用外部服务
@FeignClient(name = "external-service", configuration = FeignClientConfig.class)
public interface ServiceClient {
@RequestMapping(value = "/test/payments", method = RequestMethod.POST)
public void addPayment(@Valid @RequestBody AddPaymentRequest addPaymentRequest);
@RequestMapping(value = "/test/payments/{paymentId}", method = RequestMethod.PUT)
public ChangePaymentStatusResponse updatePaymentStatus(@PathVariable("paymentId") String paymentId,
@Valid @RequestBody PaymentStatusUpdateRequest paymentStatusUpdateRequest);
}
Run Code Online (Sandbox Code Playgroud)
最近3个月,我在日志文件中发现3-4次以下失败:
json.ERROR_RESPONSE_BODY:连接拒绝执行POST http:// external-service / external / payments json.message:发送付款添加付款失败出于其他原因:{ERROR_RESPONSE_BODY =连接拒绝执行POST http:// external-service / external / payments,EVENT = ADD_PAYMENT_FAILURE,TRANSACTION_ID = XXXXXXX} {} json.EVENT:ADD_PAYMENT_FAILURE json.stack_trace:feign.RetryableException:连接拒绝执行POST http:// external-service / external / payments 在feign.FeignException.errorExecuting(FeignException.java:67)在feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104)在feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)在feign.ReflectiveFeign $ FeignInvocationHandler.invoke(Reflective) Java:103)
是否可以在Feign客户端上添加Spring Retry。我想用什么来注释addPayment操作
@Retryable(value …Run Code Online (Sandbox Code Playgroud) spring spring-retry spring-boot spring-cloud spring-cloud-feign
在我的应用程序中,我需要调用外部端点,如果速度太慢,则会激活回退.
以下代码是我的应用程序的示例:
@FeignClient(name = "${config.name}", url = "${config.url:}", fallback = ExampleFallback.class)
public interface Example {
@RequestMapping(method = RequestMethod.GET, value = "/endpoint", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
MyReturnObject find(@RequestParam("myParam") String myParam);
}
Run Code Online (Sandbox Code Playgroud)
它的后备实施:
@Component
public Class ExampleFallback implements Example {
private final FallbackService fallback;
@Autowired
public ExampleFallback(final FallbackService fallback) {
this.fallback = fallback;
}
@Override
public MyReturnObject find(final String myParam) {
return fallback.find(myParam);
}
Run Code Online (Sandbox Code Playgroud)
此外,配置的断路器超时:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
如何实现集成测试以检查我的电路中断是否正常工作,即,如果我的端点(在这种情况下是模拟的)很慢或者它是否返回了像4xx或5xx这样的错误?
我正在使用带有Spring Cloud的Spring Boot 1.5.3(Feign + Hystrix)
testing spring-boot hystrix spring-cloud-feign spring-cloud-netflix
我知道我们可以通过提供 url Ex 来强制 FeignClient 使用 OkHttp 而不是 Ribbon。 @FeignClient(url="serviceId", name="serviceId")
即使只提供了名称,我也希望使用 OkHttpClient。前任。@FeignClient(name="serviceId")
根据 spring cloud 文档“如果启用 Ribbon,则它是 LoadBalancerFeignClient,否则使用默认的 feign 客户端。”
如何禁用功能区以便使用默认的伪装客户端。
spring-cloud netflix-feign spring-cloud-feign feign spring-cloud-netflix
我创建了一个 FeignClient 并添加了 ConditionalOnExpression,但是添加 ConditionalOnExpression 后,FeignClient 未加载,并抱怨没有 MyFeignClient 类型的合格 bean。如果我删除 ConditionalOnExpression,并且 ConditionalOnExpression 在其他组件中返回 true,则它工作正常。
@ConditionalOnExpression("'${com.service}'.contains('MANISH')")
@FeignClient(name = "client", url = "${com.api-url}}")
public interface RacingMeetingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "${com.my-endpoint")
String getRestResult();
}
Run Code Online (Sandbox Code Playgroud) 我在 azure 应用程序服务中部署了一个 Spring Boot 应用程序。它有一个 Rest api 定义如下:
@RestController
public class TestController {
@PostMapping("/test-api")
public TestDTO test() {
TestDTO testDTO = testService.createTest();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,从在本地运行的另一个 Spring Boot 应用程序,我尝试使用 feign 客户端调用该服务。
@FeignClient(value = "${test.service.name}", url = "${test.service.url}")
public interface TestClient {
@PostMapping("/test-api")
TestDTO test();
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
# feign client urls
test.service.name=test-service
test.service.url=https://my-app.azurewebsites.net
Run Code Online (Sandbox Code Playgroud)
当我在 feign 客户端中调用该方法时,出现此异常:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) ~[na:1.8.0_222]
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) ~[na:1.8.0_222]
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) ~[na:1.8.0_222]
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392) ~[na:1.8.0_222]
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302) ~[na:1.8.0_222] …Run Code Online (Sandbox Code Playgroud) ssl certificate spring-boot spring-cloud-feign azure-appservice
我正在尝试组合 2 个或更多服务,并且我想检查它们的状态并返回自定义响应。例如,其中一个返回 200,另一个返回 500、404、400 等。在这种情况下,我想返回空列表。下面的示例仅当所有服务返回 200 时才有效
@RestController
@RequiredArgsConstructor
public class Demo3Controller {
private final Demo1Client demo1Client;
private final Demo2Client demo2Client;
@GetMapping("/demo3")
public String get(){
return demo1Client.getDemo1() + "&&" + demo2Client.getDemo2();
}
}
Run Code Online (Sandbox Code Playgroud) 我有几个 Feign 客户端,具有不同的配置。常见的配置如下所示
public class FeignLogConfig {
@Bean
public LogOkHttpInterceptor LogOkHttpInterceptor() { //custom interceptor
return new LogOkHttpInterceptor();
}
@Bean
public feign.okhttp.OkHttpClient okHttpClient(LogOkHttpInterceptor interceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(interceptor);
return new feign.okhttp.OkHttpClient(builder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
可以进一步扩展
public class FeignRetryerConfig extends FeignLogConfig {
@Bean
public Retryer retryer() {
return new Retryer.Default(100, 500, 5);
}
}
Run Code Online (Sandbox Code Playgroud)
或者简单地
public class FeignEmptyConfig extends FeignLogConfig {}
Run Code Online (Sandbox Code Playgroud)
客户端注释为
@FeignClient(value = "retryClient", url = url, configuration = FeignRetryerConfig.class)
Run Code Online (Sandbox Code Playgroud)
或者
@FeignClient(value = "logClient", url = url, configuration = …Run Code Online (Sandbox Code Playgroud) spring-boot ×8
spring-cloud ×4
feign ×3
spring ×3
java ×2
certificate ×1
hystrix ×1
kubernetes ×1
resttemplate ×1
spring-retry ×1
ssl ×1
testing ×1