Spring cloud eureka是用來實現服務的註冊和發現;既包含了服務端組件也包含了客戶端組件。
Eureka的服務端也成爲服務註冊中心,支持高可用配置,Eureka客戶端主要處理服務的註冊與發現,Eureka的客戶端向註冊中心註冊自身提供的服務並週期性的發送心跳來更新它的服務,也能從服務端查詢當前註冊的服務信息並把它們緩存到本地並週期性地刷新服務狀態。
本篇文章主要分爲以下幾個方面:
1、註冊中心搭建
2、服務註冊
3、高可用註冊中心
4、消費服務
一、註冊中心搭建:
使用IDEA創建一個springboot項目,勾選eureka server:
在主類中添加註解@EnableEurekaServer,啓動一個服務註冊中心,供其他應用進行對話
@EnableEurekaServer
@SpringBootApplication
public class Eurekaserver1Application {
public static void main(String[] args) {
SpringApplication.run(Eurekaserver1Application.class, args);
}
}
在配置文件添加:
server.port=8088
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url..defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
默認情況下服務註冊中心會將自己作爲客戶端來嘗試註冊他自己。
eureka.client.register-with-eureka:false:不向註冊中心註冊自己
eureka.client.fetch-registry:false:由於註冊中心的職責是維護實例,並不需要去檢索服務,所以設置成false。
二、註冊服務提供者:
新創建一個springboot項目,勾選Eureka Discovery:
新建一個controller:
@RequestMapping(value = "/")
@RestController
public class HelloController {
@RequestMapping(value = "hello")
public String hello(){
return "hello world";
}
}
在application上添加註解:@EnableDiscoveryCilent
@EnableDiscoveryClient
@SpringBootApplication
public class Helloserver1Application {
public static void main(String[] args) {
SpringApplication.run(Helloserver1Application.class, args);
}
}
配置文件:
server.port=8090
spring.application.name=helloservice-1
eureka.client.service-url.defaultZone=http://localhost:8088/eureka
指定了服務註冊中心
然後啓動項目,訪問註冊中心,就會看到服務以及註冊到註冊中心了。
三、高可用服務註冊中心
在微服務架構的分佈式環境下,我們必須要考慮發生故障的情況,所以在生成環境中必須對各個組件進行高可用部署,接下來我們進行註冊中心的高可用,構建一個雙節點的服務註冊中心;
改造之前的服務註冊中心,修改配置文件內容:
server.port=8088
spring.application.name=server-1
eureka.instance.hostname=server-1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url..defaultZone=http://server-2:8089/eureka/
在新建一個springboot項目 作爲eureka server,配置文件如下:
server.port=8089
eureka.instance.hostname=server-2
spring.application.name=server-2
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://server-1:8088/eureka/
訪問註冊中心2:
訪問註冊中心1:
高可用搭建完成。
四、消費服務(Ribbon)
新建一個springboot項目,勾選 Eureka Discovery和 Ribbon
在主類中添加註解、引入RestTempate
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
配置文件:
server.port=8091
spring.application.name=ribbon-consumer
eureka.client.service-url.defaultZone=http://server-2:8089/eureka/
eureka.client.service-url.defaultZone 指定 註冊1、2都可以。
啓動項目,再查看註冊中心:
多了一個RIBBON-CONSUER服務,說明已經成功註冊到註冊中心;現在開始消費服務HELLOSERVICE-1
新建一個controller:
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer")
public String getvalue(){
return restTemplate.getForEntity("http://HELLOSERVICE-1/hello",String.class).getBody();
}
}
啓動項目,訪問localhost:8091/consumer,
消費服務成功,Ribbon是客戶端負載均衡,我們爲了驗證,再新建一個HELLOSERVICE,和上面的HELOSERVICE只是端口不一樣。
server.port=8093
spring.application.name=helloservice-1
eureka.client.service-url.defaultZone=http://localhost:8088/eureka
只是端口號從8090變成了8093
啓動項目,查看註冊中心:
HELLOSERVICE-1 啓動了兩個實例,只是端口不一樣,現在我們再通過consumer來消費服務,多訪問幾次,可以看到,兩個不同端口的服務交替響應:
實現了客戶端的負載均衡。
關於服務治理和Ribbon的其他內容我們後續再詳細講解。。。。。