当我尝试运行 Spring Boot 应用程序时,出现此异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3764951d]
Run Code Online (Sandbox Code Playgroud)
我认为是版本不兼容。我在我的 pom.xml 中导入了 open feign,之后它不起作用,但我不知道如何解决这个问题。我使用 open feign 2.2.5.RELEASE。这是我的 pom.xml:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@3764951d]
Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Feign 客户端,它使用 POST 将 POJO 对象发送到远程端点,并且我的应用程序启动失败,出现以下异常。
java.lang.reflect.InaccessibleObjectException:无法使字段静态最终 java.lang.invoke.MethodHandles$Lookup java.lang.invoke.MethodHandles$Lookup.IMPL_LOOKUP 可访问:模块 java.base 不“打开 java.lang.invoke”到未命名模块@420a85c4
Below the dependencies I am using in my application.
java version: 17
spring boot version: 2.5.3
spring boot cloud version: 2020.0.3
spring boot starter openfeign version: 2.2.8.RELEASE
As recommended in https://github.com/OpenFeign/feign/issues/935, I had tried
workaround solution: Adding this jvm option '--add-opens java.base/java.lang.invoke=ALL-
UNNAMED' worked.
Run Code Online (Sandbox Code Playgroud)
除 jvm 参数之外的任何其他替代建议都是最受欢迎的。
我正在使用spring-cloud-starter-openfeign
client 编写 Eureka 客户端应用程序。这是我的POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-registration-and-discovery-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-registration-and-discovery-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> …
Run Code Online (Sandbox Code Playgroud) spring-cloud netflix-eureka spring-cloud-feign feign openfeign
由于某种原因,我需要调用 GET 方法 API 并为其传递 json 请求正文。我实在找不到这样的例子。我想知道是否支持使用 feign。我怎样才能使用 feign 做到这一点?
我尝试在 Spring 中为 REST 服务控制器创建一个假客户端。
@PostMapping("/search")
public Page<MeasureDto> searchMeasures(@RequestBody MeasureDto example, Pageable pageable) {
...
}
Run Code Online (Sandbox Code Playgroud)
客户端看起来像这样:
@PostMapping("/search")
public Page<MeasureDto> searchMeasures(@RequestHeader("apiKey") String apiKey, @RequestBody MeasureDto example, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
运行测试时抛出以下异常:
引起原因:java.lang.IllegalStateException:方法有太多主体参数:公共抽象org.springframework.data.domain.Page com.foo.bar.jobservice.client.MeasureServiceClient.searchMeasures(java.lang.String,com.example .foo.jobservice.client.dto.MeasureDto,org.springframework.data.domain.Pageable)
我已经知道/尝试过的:
github上有一个已关闭的问题:https://github.com/spring-cloud/spring-cloud-netflix/issues/556
应该已经解决问题的提交问题:
https://github.com/spring-cloud/spring-cloud-openfeign/issues/26
提交:
我配置了什么:
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.PageJacksonModule;
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@EnableFeignClients
@Configuration
public class FeignConfig {
@Bean
public PageJacksonModule pageJacksonModule() {
return new PageJacksonModule();
}
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean …
Run Code Online (Sandbox Code Playgroud) 有什么区别:
当我尝试使用 openfeign 遵循教程时发现了这个问题@FeignClient(name=...)
。不过我用的是feign,不是openfeign,这个注解也不一样。
这不是同一件事吗?
到目前为止,我们有一个 feign 客户端,如果出现异常,我们曾经重试如下
Retryer<ClientResponse> retryer = RetryerBuilder.<ClientResponse>newBuilder()
.retryIfExceptionOfType(FeignException.class)
.withStopStrategy(StopStrategies.stopAfterAttempt(retryCount))
.withWaitStrategy(WaitStrategies.exponentialWait(maxWaitSeconds, TimeUnit.SECONDS))
.build();
retryer.call(() -> {
return client.doStuffs(someInput); }
);
Run Code Online (Sandbox Code Playgroud)
最近我尝试从这个自定义重试器移动到一个内置的假装重试器,如下所示:
Feign client = Feign.builder()
.decoder(jacksonDecoder)
.encoder(jacksonEncoder)
.logger(slf4jLogger)
.client(okHttpClient)
.retryer(new Retryer.Default(
SECONDS.toMillis(minWaitSeconds),
SECONDS.toMillis(maxWaitSeconds),
retryCount
))
.requestInterceptor(new BasicAuthRequestInterceptor(clientConfig.getUser(), clientConfig.getPassword()))
.target(target);
client.doStuffs(someInput);
Run Code Online (Sandbox Code Playgroud)
理解是假装客户端本身会处理异常,但显然,情况并非如此,当客户端抛出 a 时5xx
,我得到一个异常而没有重试。实现重试是否还需要其他东西?
这项服务在 dropwizard 中,git 和 SO 线程主要围绕 spring/ribbon 而我不是这种情况。
深度
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我有一个 Feign 客户端来访问需要两个标头的 createUser 端点:用户名和密码。我知道如何添加一个标头,但如何向请求添加两个标头?
@FeignClient(name = "client", url = "https://abc.abc.com/user/", configuration = FeignClientConfig.class)
public interface MyFeignClient {
@Headers("username_header: {username}") // how do I add "password" here.
@PostMapping(value = "v1/users")
void createUser((@Param("username") String username, User userRequest);
Run Code Online (Sandbox Code Playgroud)
}
更新:现在根据下面的答案,我将界面主体更改为:
@Headers({"username_header: {username}", "password_header: {password}"})
@PostMapping(value = "v1/users")
void createUser(@Param("username") String username,
@Param("password") String password,
User userRequest);
Run Code Online (Sandbox Code Playgroud)
调用它的代码是:
feignClient.createUser("me", "123", userObj);
Run Code Online (Sandbox Code Playgroud)
然后我收到错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean,
nested exception is java.lang.IllegalStateException: Method has too many Body parameters:
feignClient.createUser(java.lang.String,java.lang.String, User)
Run Code Online (Sandbox Code Playgroud) 我已经编写了一个FeignClient
,我想使用单元测试来测试它是否有效。(就我而言,集成测试不是当前开发阶段的正确方法)。
在我的测试中,FeignClient 未初始化 ( null
):NullPointerException
运行测试时我收到一条消息。
如何成功自动装配 a FeignClient
?
假冒客户:
package com.myapp.clients;
import com.myapp.model.StatusResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="myClient", url="${feign.clients.my.url}")
public interface myClient {
@RequestMapping(method= RequestMethod.GET, value="/v1/users/{userId}")
StatusResponse getStatus(
@RequestHeader(value = "Auth", required = true) String authorizationHeader,
@RequestHeader(value = "my_tid", required = true) String tid,
@PathVariable("userId") String userId);
}
Run Code Online (Sandbox Code Playgroud)
测试:
package com.myapp.clients;
import com.intuit.secfraudshared.step.broker.model.StatusResponse;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class MyClientTest {
@Autowired
MyClient myClient; …
Run Code Online (Sandbox Code Playgroud) 我使用 feign 客户端在微服务之间进行休息调用,当我使用 IntelliJ IDEA 执行它时,它工作正常。但如果我用 jar 执行它,它会给我以下错误。
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.springframework.cloud.netflix.eureka.loadbalancer.EurekaLoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnBeanCondition]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:155)
at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:108)
at org.springframework.cloud.context.named.NamedContextFactory.getInstances(NamedContextFactory.java:203)
at org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient.lambda$execute$2(RetryableFeignBlockingLoadBalancerClient.java:118)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:225)
at org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient.execute(RetryableFeignBlockingLoadBalancerClient.java:113)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
at com.sun.proxy.$Proxy76.fundTransfer(Unknown Source)
at com.core.banking.user.service.internetbanking.acl.service.FundTransferService.fundTransfer(FundTransferService.java:19)
at com.core.banking.user.service.internetbanking.acl.event.EventHandler.lambda$onEvent$0(EventHandler.java:38)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1736)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1728)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused …
Run Code Online (Sandbox Code Playgroud) openfeign ×10
java ×6
feign ×5
spring-boot ×4
spring ×3
maven ×2
spring-cloud ×2
dropwizard ×1
openjdk-17 ×1
oracle ×1