1、Feign消费服务,程序入口类HiController启动时始终无法注入scheduleServiceHi。报错如下:
Description:
Field scheduleServiceHi in com.example.servicefeign.controller.HiController required a bean of type 'com.example.servicefeign.service.ScheduleServiceHi' 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.example.servicefeign.service.ScheduleServiceHi' in your configuration.
程序入口类HiController代码如下:
package com.example.servicefeign.controller;
import com.example.servicefeign.service.ScheduleServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@Autowired
private ScheduleServiceHi scheduleServiceHi;
@RequestMapping(value = "/hi")
public String sayHi(@RequestParam String name){
return scheduleServiceHi.sayHiFromClientOne(name);
// service-hi服务中默认的方法
// return scheduleServiceHi.home();
}
}
被调用接口ScheduleServiceHi 代码如下:
package com.example.servicefeign.service;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-hi")
//@EnableFeignClients(value ="service-hi" )
public interface ScheduleServiceHi {
@RequestMapping(value = "/Hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
//service-hi服务中默认的方法
// String home();
}
排查了下才发觉是,@FeignClient 与@EnableFeignClients两个注解搞错了。应该使用@FeignClient。但却错误的使用了@EnableFeignClients。特此记录之。
2、被调用接口ScheduleServiceHi 中
String sayHiFromClientOne(@RequestParam(value = "name") String name);
为追加的方法。而String home();
为服务提供方service-hi
定义的方法。程序入口类HiController调用两者均可成功消费服务。
服务提供方service-hi代码:
package com.test.example.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class EurekaClientServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping(value = "/Hi")
public String home (@RequestParam(value = "name",defaultValue = "Kobe")String name){
return "Hi," + name+"I'm from port:"+ port;
}
}