标签: openfeign

java.lang.IllegalStateException:没有定义用于负载平衡的假客户端。您是否忘记包含 spring-cloud-starter-netflix-ribbon ?

我得到了例外

FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1654)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1174)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:257)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1012)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:338)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)
    at com.hsbc.gbgcf.spartan.referencedatabase.UserRegistrationApplication.main(UserRegistrationApplication.java:57)
Run Code Online (Sandbox Code Playgroud)

执行我的项目时。我的 pom.xml 包含

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
Run Code Online (Sandbox Code Playgroud)

我也在我的其他项目中使用 feign 客户端,没有任何额外的功能区依赖,这些都使用相同的 spring-boot 版本 2.0.8

我已经针对相同的问题提到了其他堆栈溢出链接,他们要求添加功能区的其他依赖项。我曾尝试在我的 pom.xml 中添加相同的内容,但没有帮助。

FeignClient 接口是,

@FeignClient(value = "user-service", decode404 = true)
public interface UserFeignClient {

    @PostMapping("/do-something")
    void doSomething();
}
Run Code Online (Sandbox Code Playgroud)

主类代码,

@Configuration
@EnableAspectJAutoProxy …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot openfeign

6
推荐指数
3
解决办法
9325
查看次数

Spring Boot OpenFeign 随机端口测试

我有一个 OpenFeign 客户端设置如下:

@FeignClient(name = "myService", qualifier = "myServiceClient", url = "${myservice.url}")
public interface MyServiceClient {
...
}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 测试设置如下:

@SpringBootTest(webEnvironment = RANDOM_PORT, classes = MyApplication.class)
@RunWith(SpringRunner.class)
@EnableFeignClients(clients = MyServiceClient .class)
public class ReservationSteps {
...
}
Run Code Online (Sandbox Code Playgroud)

该测试应该启动应用程序并使用 Feign 客户端向其发送请求。

问题是 RANDOM_PORT 值。

如何在属性文件中声明“myservice.url”属性,以便它包含正确的端口?

我已经尝试过这个:

myservice.url=localhost:${local.server.port}
Run Code Online (Sandbox Code Playgroud)

但结果是“localhost:0”。

我不想为端口使用常量值。

请帮忙。谢谢!

java spring-boot spring-boot-test openfeign

5
推荐指数
1
解决办法
1602
查看次数

Spring + OpenFeign:POST 结果出现 NoSuchBeanDefinitionException

我已经为此苦苦挣扎了一段时间了。简而言之,问题是每当我POST使用 OpenFeign 发出请求时,都会收到以下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)

首先,我build.gradle直接从 Spring Initializr 生成,包括 Eureka Discovery Client、Hystrix、Lombok、OpenFeign 和 Spring Reactive Web:

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example.feign'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Hoxton.SR1")
}

dependencies { …
Run Code Online (Sandbox Code Playgroud)

spring netflix-eureka openfeign

5
推荐指数
1
解决办法
5774
查看次数

OAuth2FeignRequestInterceptor 类在 Spring Boot 2.3 中已弃用

在我们最后的伪装客户端安全配置中,我们有这个 Bean:

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(
            ClientCredentialsResourceDetails oauth2RemoteResource) {
        return new OAuth2FeignRequestInterceptor(
                new DefaultOAuth2ClientContext(),
                oauth2RemoteResource
        );
    }
Run Code Online (Sandbox Code Playgroud)

在 2.3 spring 版本中OAuth2FeignRequestInterceptor已弃用!但是我们找不到新的。

有人知道吗?

single-sign-on spring-boot spring-security-oauth2 openfeign

5
推荐指数
1
解决办法
2846
查看次数

内容类型标头值导致 UnsupportedMediaType 415 OpenFeign

我们最近从版本 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)
我们的 Feign 接口定义如下:
@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 spring-boot spring-cloud feign openfeign

5
推荐指数
0
解决办法
832
查看次数

使用 Feign 构建器请求不会向子客户端发送 Trace-id、span-id,但使用 Rest 模板会显示子客户端上的所有标头

我正在使用 Feign Builder 发出顺序请求。请求标题中没有 x-b3-traceid,x-b3-spanid .. 。这就是为什么我的最后一个客户的日志出现在 zipkin 上。

我使用 spring boot 2.4.2 、 spring cloud 2020.0.0 、 feign-core 10.10.1 、 feign-okhttp 10.10.1 。我尝试过 spring-cloud-openfeign 并取得了想要的结果。但我不想使用这个库。这里有使用 Feign Builder 和 Rest Template 时的请求。我在 zipkin 上没有看到相同的日志。

我的 Client1 应用程序。我正在发送请求 http://localhost:8082/

import feign.Client;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sample.feign2.Client2Feign;

import javax.servlet.http.HttpServletRequest;
@RestController
public class SampleController {

    private Logger logger …
Run Code Online (Sandbox Code Playgroud)

zipkin spring-boot spring-cloud-sleuth openfeign

5
推荐指数
1
解决办法
2854
查看次数

模拟 OpenFeign 客户端以在 Spring 库中进行单元测试,而不是在 Spring Boot 应用程序中进行单元测试

我已经实现了一个基于此官方存储库调用 get API 的 feign 客户端。我有一个规则类UserValidationRule,需要调用它来获取 API 调用getUser()并验证一些内容。这按预期工作,但是当我开始测试该规则类时,模拟假客户端并不成功,它会继续调用实际的 API。我已经简化了情况,所以请忽略简单性哈哈。这是我发现这个stackoverflow问题后的一个后续问题

API 返回此模型:

@Data
public class userModel {
    private long id;
    private String name;
    private int age;

}
Run Code Online (Sandbox Code Playgroud)

与其余客户端方法的接口:

public interface UserServiceClient {

    @RequestLine("GET /users/{id}")
    UserModel getUser(@Param("id") int id);
}
Run Code Online (Sandbox Code Playgroud)

在规则类中,我构建了 feign 客户端并调用 API:

@RequiredArgsConstructor
@Component
public class UserValidationRule {

    private static final String API_PATH = "http://localhost:8080";

    private UserServiceClient userServiceClient;


    public void validate(String userId, ...) {
            // some validations 
            validateUser(userId);

    }

    private void validateUser(String userId) …
Run Code Online (Sandbox Code Playgroud)

java spring microservices spring-cloud-feign openfeign

5
推荐指数
1
解决办法
1万
查看次数

如何将自定义Http客户端注入Spring Cloud openfeign?

我正在尝试向 Spring Cloud OpenFeign 提供 CloseableHttpClient。 Spring Cloud Open Feign 文档表示它支持 CloeableHttpClient。Spring 文档没有给出任何实际替换 HTTP 客户端的示例。

基本上,我向 HTTP 客户端提供 SSLContext,并且我希望 Feign 使用此 SSLContext 加载的客户端。如何将这个CloseableHttpClient注入到Feign中呢?

以下是我的相关配置:

  1. 我正在使用 SpringBootApp
@SpringBootApplication
@EnableFeignClients
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
Run Code Online (Sandbox Code Playgroud)
  1. Feign客户端界面如下:
import org.springframework.cloud.openfeign.FeignClient;
//skipping rest of the imports for brevity
    @FeignClient(name ="remote-service", url = "${remote.service-url}", configuration = FeignConfig.class)
        public interface RemoteServiceApi {
            @GetMapping(value = "/api/v1/resources/{Id}")
            public String getResource(@PathVariable("Id") String Id);
        }
Run Code Online (Sandbox Code Playgroud)
  1. FeignConfig 类
import org.apache.http.impl.client.CloseableHttpClient;
//skipping …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot openfeign

5
推荐指数
1
解决办法
5286
查看次数

如何让多个 Fe​​ignClient-s 使用相同的 serviceId/name?

假设有 2 个端点:

@RequestMapping("/ep1")
interface Endpoint1 {
    @GetMapping("/echo")
    String echo();
}

@RequestMapping("/ep2")
interface Endpoint2 {
    @GetMapping("/echo")
    String echo();
}
Run Code Online (Sandbox Code Playgroud)

在后端,两者都在同一服务器中运行,该服务器在 Eureka 或 Consul 中注册为 serviceId="MY-SERVER" 或...

@RestController
public class Endpoint1Controller implements Endpoint1 {
    public String echo() {
        return "echo from Endpoint1"
    }
}
@RestController
public class Endpoint2Controller implements Endpoint2 {
    public String echo() {
        return "echo from Endpoint2"
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的 2 个 FeignClients 必须共享相同的名称/serviceId 才能发现 Eureka/Consul 中的服务,但每个 feign 客户端的 serviceId 应该是唯一的!……这该怎么处理?

@FeignClient("MY-SERVER")
public interface Endpoint1Client extends Endpoint1 {
} …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-cloud-feign feign openfeign

5
推荐指数
1
解决办法
4307
查看次数

替换 org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory

当我升级implementation 'org.springframework.cloud:spring-cloud-openfeign-core:2.2.2.RELEASE'到最新版本时implementation 'org.springframework.cloud:spring-cloud-openfeign-core:3.1.1'

我收到此导入类的错误:

import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;

@Bean
  public Client client(
      HttpClientConnectionManager httpClientConnectionManager,
      CachingSpringLoadBalancerFactory lbClientFactory,
      SpringClientFactory clientFactory) {

    CloseableHttpClient closeableHttpClient = HttpClients.custom()
        .setConnectionManager(httpClientConnectionManager)
        .build();
    ApacheHttpClient delegate = new ApacheHttpClient(closeableHttpClient);
    return new LoadBalancerFeignClient(delegate, lbClientFactory, clientFactory);
  }
Run Code Online (Sandbox Code Playgroud)

你知道我必须如何更换它们才能使用最新版本吗?

spring spring-boot feign openfeign

5
推荐指数
1
解决办法
2891
查看次数