spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.22k stars 786 forks source link

Could not autowire @FeignClient #114

Closed alexciornii closed 5 years ago

alexciornii commented 5 years ago

I try to autowire feign client into the service, but the error is Could not autowire. No beans of 'OperatorClient' type found. Spring cloud version Finchley.SR2 Spring boot version 2.0.6.RELEASE

That is pom:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://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.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spboot</groupId>
    <artifactId>cloudexample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloudexample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <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>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>bintray-kptfh-feign-reactive</id>
            <name>bintray</name>
            <url>https://dl.bintray.com/kptfh/feign-reactive</url>
        </repository>
    </repositories>

</project>

That is main class:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class CloudExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudExampleApplication.class, args);
    }

}

FeignClient:

@FeignClient(name = "cloud-example")
public interface OperatorClient {

    @RequestMapping(value = "/cloud-example/users", method = RequestMethod.GET)
    String getUsers();

}

Controller:

@RestController
public class OperatorController {

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String getUsers() {
        return "Man";
    }

}

Service:

@Data
@Service
public class CloudService {

    @Autowired
    private OperatorClient client;

    public String getName() {
        return client.getUsers();
    }
}

Another Controller:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private CloudService service;

    @GetMapping("/hello")
    public String getUser() {
        return "Hello " + service.getName() + "!";
    }
}

So, in CloudService the OperatorClient isn't autowiring.

ryanjbaxter commented 5 years ago

Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file.

alexciornii commented 5 years ago

@ryanjbaxter Sorry, but maybe i didn't put correctly the question. I try to autowire in project with version of spring boot 2.0.6.RELEASE and spring-cloud Finchley.SR2 feign client from project with version of spring boot 1.4.2.RELEASE and spring-cloud Camden.SR3. It's not autowiring! Spring Boot magic can't find feign clients from version below. Is any idea how can resolve it?

P.S. If it still need a sample, i will prepare it.

ryanjbaxter commented 5 years ago

Yes please provide a sample

alexciornii commented 5 years ago
  1. example of feign client consumer with version of spring boot 2.0.6.RELEASE and spring-cloud Finchley.SR2 https://github.com/alexciornii/cloud-example
  2. example of feign client consumer with version of spring boot 1.5.19.RELEASE and spring cloud Camden.SR3 https://github.com/alexciornii/feignconsumerexample
  3. example of feign client producer with version of spring boot 1.4.2.RELEASE Camden.SR3 https://github.com/alexciornii/feignexample

in project in p.1 can't autowiring feign client from project in p.3. But in project p.2 that's working fine. I think there was a conflict between versions. Is any way to autowiring feign client from below versions?

spencergibb commented 5 years ago

Camden and Dalston are both end of life. Can you retry with Edgware?

alexciornii commented 5 years ago

@spencergibb No. The same problem.

ryanjbaxter commented 5 years ago

Please update your samples to use the latest Edgware or Finchley releases.

alexciornii commented 5 years ago

@ryanjbaxter But i need it to be Camdem.

spencergibb commented 5 years ago

Camden is no longer supported

alexciornii commented 5 years ago

@spencergibb That's is understood. But if i have any project which is using Cadem.SR3 dependencies i can't use their feign clients in project with version above?

ryanjbaxter commented 5 years ago

We are just not supporting Camden or Dalston any longer so if you can reproduce it with Edgeware than we can try and fix it there.

alexciornii commented 5 years ago

@ryanjbaxter Reproduced with Edgware.SR5 https://github.com/alexciornii/feignexample https://github.com/alexciornii/cloud-example

spencergibb commented 5 years ago

Unfortunately, you can't use an app with Finchley and import Edgware feign clients as the package of @FeignClient changed with the major release. They will all need to be Edgware or Finchley.

alexciornii commented 5 years ago

Thanks.

apostrophe commented 5 years ago

Make sure your import is correct: import org.springframework.cloud.openfeign.FeignClient;

NOT: import org.springframework.cloud.netflix.feign.FeignClient;

leimbag commented 4 years ago

The same problem. And you can use @EnableFeignClients(basePackages = "com.xxx.xxx"), for scan feign client

LiYeLin commented 4 years ago

add(require = false)after your @Autowired, that can solve the problem ,But it didn't address the root cause

ankit5174 commented 4 years ago

Same Problem when using Feign Client inside micronaut service. Does anyone has a workaround

spencergibb commented 4 years ago

I doubt feign client works in micronaut.

ankit5174 commented 4 years ago

@spencergibb Do you know any reason why it cannot work in micronaut. According to my knowledge, micronaut uses Factory to create a instance of a external or dependency Bean(In in this eg CloudService). But DI wont allow OperatorClient to be injected since we are using new to create instance of CloudService. Let me know if my understanding is correct.

spencergibb commented 4 years ago

I have no idea how micronaut implements anything spring. FeignClients are created using non-trivial proxy creation. Besides the error message, it's going to be totally unrelated to this issue.

vieiralucas89 commented 4 years ago

The same problem. And you can use @EnableFeignClients(basePackages = "com.xxx.xxx"), for scan feign client

Thank you, here working.

jackDing2016 commented 3 years ago

Make sure your import is correct: import org.springframework.cloud.openfeign.FeignClient;

NOT: import org.springframework.cloud.netflix.feign.FeignClient;

Thanks!

theMentatHenrique commented 10 months ago

Hi, i resolve this problema using annotation @EnableFeignClients on applicationClass i follow this documentation : https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html