1. 程式人生 > >Finchley.SR1版本的[email protecte

Finchley.SR1版本的[email protecte

我是跟著尚矽谷的SpringCloud教程學的,然後在寫程式碼的時候用了比較新的版本去替代視訊裡的教學版本。

這個問題出現在引入feign元件的時候。

使用SpringCloud的Finchley.SR1版本

引用openfeign替代了 feign,原因如下

大意是說feign是舊版本,16年就不再更新了,現在SpringCloud使用openfein去替代feign

1,在公用service介面層實現@FeignClient註解

@FeignClient(value = "MICROSERVICECLOUD-DEPT")
public interface DeptClientService
{
	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Dept get(@PathVariable("id") long id);

	@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
	public List<Dept> list();

	@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
	public boolean add(Dept dept);
}

然後執行maven 的 clean 和 install,供其他模組呼叫

2,在feign模組的controller層呼叫 

@RestController
public class DeptController_Consumer
{
	@Autowired
	private DeptClientService service;

	@RequestMapping(value = "/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id)
	{
		return this.service.get(id);
	}

	@RequestMapping(value = "/consumer/dept/list")
	public List<Dept> list()
	{
		return this.service.list();
	}

	@RequestMapping(value = "/consumer/dept/add")
	public Object add(Dept dept)
	{
		return this.service.add(dept);
	}
}

此時IDEA針對service報錯:Could not autowire. No beans of 'DeptClientService' type found. less... (⌘F1)  Inspection info:Checks autowiring problems in a bean class.

這裡報錯不影響程式碼執行,但是我沒理解深層原因。按說正常呼叫@Autowired

不會報錯。

feign主啟動類類如下

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.spring.microservicecloudconsumerdeptfeign"})
@ComponentScan("com.atguigu.spring.microservicecloudconsumerdeptfeign")
public class MicroservicecloudConsumerDeptFeignApplication {

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

註解@ComponentScan報紅,提示@SpringBootApplication註解已經掃描過該包,但不影響程式執行

啟動之後直接掛掉,報錯提示如下

Description:

Field service in com.atguigu.spring.microservicecloudconsumerdeptfeign.controller.DeptController_Consumer required a bean of type 'com.atguigu.spring.microservicecloudapi.service.DeptClientService' that could not be found.


Action:

Consider defining a bean of type 'com.atguigu.spring.microservicecloudapi.service.DeptClientService' in your configuration.

應該是service沒有注入進去,找不到。

這個階段我還分析不了原因,所以搜了一下,找到一個可能的解決方式

於是把主啟動類註解改成

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.spring.microservicecloudapi.service","com.atguigu.spring.microservicecloudconsumerdeptfeign"})
public class MicroservicecloudConsumerDeptFeignApplication {

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

專案正常啟動,訪問正常