1、配置Eureka注册中心eureka-server,pom依赖如下:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
2、Eureka Server实现代码,重点是其中的@EnableEurekaServer注解,后续博文会对设计注解的实现原理进行分析,本篇文章不涉及
@SpringBootApplication
@EnableEurekaServer
public class SpringbootdemoServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoServerApplication.class, args);
}
}
3、Eureka Server配置文件:application.yml
spring:
application:
name: spring-boot-demo-server
server:
#服务注册中心端口号
port: 8001
eureka:
instance:
#服务注册中心实例的主机名
hostname: localhost
client:
#是否向服务注册中心注册自己
register-with-eureka: false
#是否检索服务
fetch-registry: false
service-url:
#服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://localhost:8001/eureka/
4、配置服务提供者pom依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
5、服务提供者实现类
@RestController
public class HelloController {
@RequestMapping("/info")
public String Hello() {
return "hello adminDemo2,this is a spring-boot-client-demo2 project";
}
/*
服务提供者provider2
*/
@RequestMapping("/producerHello")
public String Hello(@RequestParam("name") String name) {
return "hello " + name + ",this is spring-boot-client-demo2 project";
}
}
6、服务提供者配置文件application.yml
spring:
application:
name: spring-boot-demo-client
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
7、服务提供者同样需要步骤2中的启动类,只是注解不一样@EnableEurekaClient
8、消费者配置:
8-1、消费者配置服务接口:
@Service
@FeignClient(name = "spring-boot-demo-client")
public interface IHelloRemote {
//需要匹配服务提供者接口名称
@RequestMapping(value = "/producerHello")
public String sayHello(@RequestParam(value = "name") String name);
}
8-2、消费者实现类:注意:还需要制定扫描的包名@EnableFeignClients(basePackages = "com.example.api")我代码中使用的是@ComponentScan
@RestController
@ComponentScan(basePackages = { "com.example.api", "com.example.demo" })
public class ConsumerCtrlDemo {
@Autowired
private IHelloRemote callHelloService;
/*
消费者的接口,去调用服务提供者
问题:只能使用@RequestMapping("/consumerHello/{name}") @PathVariable("name")方法 不知道有没有其它方式代替?
*/
@RequestMapping("/consumerHello/{name}")
public String index(@PathVariable("name") String name) {
return callHelloService.sayHello(name);
}
//有问题的
// @RequestMapping("/consumerHello2")
// public String index2(@RequestParam("name") String name){
// return helloRemote.sayHello(name);
// }
@RequestMapping("/info")
public String info() {
return " Hi,I am a consumer!";
}
}
8-3、消费者配置启动类,同步骤2:
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class SpringbootClientdemoConsumer {
public static void main(String[] args) {
SpringApplication.run(SpringbootClientdemoConsumer.class, args);
}
}
8-4、消费者配置文件:application.yml
spring:
application:
name: spring-boot-demo-consumer
server:
port: 8004
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
如上步骤配置完毕,依次启动server、服务提供者、服务消费者后,打开浏览器输入localhost:端口号/xxx,即可访问;
遇到的问题:
在配置服务消费者接口时,由于存在@serverice,且接口与@SpringBootApplication不在同一个包,或者子包内,所以导致消费者无法启动;
正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上
这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository
解决方法:两种解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解:@ComponentScan(basePackages = { "com.example.api", "com.example.demo" })或者:还需要制定扫描的包名@EnableFeignClients(
basePackages = "com.example.api")