1、配置Eureka註冊中心eureka-server,pom依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
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依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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")