java Spring Cloud Feign 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47504633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Cloud Feign Error
提问by Jeff Zhang
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'feignController': Unsatisfied dependency expressed through field 'feignService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.study.base.microservices.service.BookFeignClient': 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-ribbon?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.study.base.microservices.FeignClient.main(FeignClient.java:19) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.study.base.microservices.service.BookFeignClient': 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-ribbon?
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 19 common frames omitted
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-ribbon?
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:148) ~[spring-cloud-netflix-core-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:166) ~[spring-cloud-netflix-core-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 29 common frames omitted
BookFeignClient.java
BookFeignClient.java
@FeignClient(name="book-service")
public interface BookFeignClient {
@RequestMapping("book/view/{id}")
public Book viewBook(@RequestParam("id") String id);
}
FeignController.java
FeignController.java
@RestController
public class FeignController {
@Autowired
private BookFeignClient feignService;
@GetMapping("feign/view/{id}")
public Book viewBook(@PathVariable String id) {
return feignService.viewBook(id);
}
}
FeignClient.java
FeignClient.java
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignClient {
public static void main(String[] args) {
SpringApplication.run(FeignClient.class, args);
}
}
application.yml
应用程序.yml
server:
port: 8020
spring:
application:
name: micro-feign-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
preferIpAddress: true
ribbon:
eureka:
enabled: true
pom.xml
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Dalston.SR4
达尔斯顿.SR4
How can i fix this ?
我怎样才能解决这个问题 ?
回答by Shravan Ramamurthy
I encountered the same error recently, with spring boot 2.0.3.RELEASE. When I added openfeign dependency first, without the version, I found that Maven was not pulling down the dependency (Not sure why). So I had to define the version explicitly for maven to actually pull it. With that, I also got the spring-cloud-starter-Netflix-ribbon
transitively. At this point, when I started up the server, I got the same error specified in the first post. After adding spring-cloud-starter-Netflix-ribbon
dependency with the version in the pom, the error got resolved. So I ended up adding these 2 dependencies.
我最近遇到了同样的错误, spring boot 2.0.3.RELEASE。当我第一次添加openfeign依赖时,没有版本,我发现Maven并没有拉下依赖(不知道为什么)。所以我必须明确定义版本,以便 maven 实际拉取它。有了这个,我也得到了spring-cloud-starter-Netflix-ribbon
传递。此时,当我启动服务器时,我遇到了第一篇文章中指定的相同错误。在spring-cloud-starter-Netflix-ribbon
pom中添加与版本的依赖关系后,错误得到解决。所以我最终添加了这两个依赖项。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
回答by vsoni
Add ribbondependency in your pom.xml
.
添加丝带在你的依赖pom.xml
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
I also see some discrepancy in your BookFeignClient
. @PathVariable
should be used in place of @RequestParam
.
我也看到您的BookFeignClient
. @PathVariable
应该用来代替@RequestParam
.
@FeignClient(name="book-service")
public interface BookFeignClient {
@RequestMapping("book/view/{id}")
public Book viewBook(@PathVariable("id") String id);
}
回答by Jeff Zhang
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignClient {
public static void main(String[] args) {
SpringApplication.run(FeignClient.class, args);
}
}
change to
改成
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClient.class, args);
}
}
it work now
它现在工作
回答by Pramod S. Nikam
Try to add following code in your application class i.e. FeignClient :
尝试在您的应用程序类中添加以下代码,即 FeignClient :
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
and then check.
然后检查。
Plus I think your
另外我认为你的
@GetMapping("feign/view/{id}")
public Book viewBook(@PathVariable String id) { // pertaining to feign Controller
Should be replace with:
应该替换为:
@RequestMapping("book/view/{id}")
public Book viewBook(@RequestParam("id") String id);
回答by Cleibson
Would change the interface BookFeignClient, to version that is below, after this change Spring will not require balancing.
将接口 BookFeignClient 更改为以下版本,此更改后 Spring 将不需要平衡。
@FeignClient(name="book-service", url = "https://meu.dominio.com")
public interface BookFeignClient {
@RequestMapping("book/view/{id}")
public Book viewBook(@PathVariable("id") String id);
}
回答by Gui Alencar
I think you should add the following anotation in your proxy:
我认为您应该在代理中添加以下注释:
@FeignClient(name="book-service")
@RibbonClient(name="external-service-name") // this is missing, the name of the external service registered in your naming server
public interface BookFeignClient {
...