寫了一個Eureka註冊調用的demo,代碼大致如下:
Feign
@FeignClient(name = "spring-cloud-provider")
public interface HelloRemote {
@RequestMapping("/hello")
public String hello(@RequestParam("name")String name);
}
controller
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name){
return helloRemote.hello(name);
}
}
程序啓動時遇到:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field helloRemote in com.freemud.controller.HelloController required a bean of type 'com.freemud.controller.HelloRemote' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.freemud.controller.HelloRemote' in your configuration.
報錯大致的意思就是HelloController中@Autowired自動注入的屬性HelloRemote
無法被找到
解決:
問題應當出在Feign上,在寫@EnableFeignClients註解的時候應當加上包名,否則就會出現讀取不到的情況。