SpringCloud筆記(二)高可用註冊中心的實現
目錄
2、eureka-sever01和eureka-sever02
在微服務架構中,註冊中心是十分核心的元件,可以實現服務治理。如果註冊中心發生故障,就會導致整合微服務無法訪問,因此應該對註冊中心進行高可用的部署。
對Eureka進行高可用的思想非常簡單:就是將自己作為服務註冊給其他註冊中心,這樣就可以形成一組互相註冊的註冊中心,以實現服務清單的同步,實現高可用的目的。
在之前的部落格中SpringCloud筆記(一)服務註冊與服務發現,搭建註冊中心時,有兩個引數:
register-with-eureka: false
fetch-registry: false
都設定成了false,因為自己當時搭建的Eureka-Server本身就是註冊中心,因此就不用將自己註冊給註冊中心,而搭建高可用的註冊中心,均需要設定成true,將註冊中心當做服務註冊給其他註冊中心。
下面我們開始搭建一個高可用的註冊中心。
1、要實現的拓撲
2、eureka-sever01和eureka-sever02
要匯入的依賴在之前的部落格中已經提過這裡不再贅述。這裡主要講一下application.yml怎樣配置。
eureka-server01的application.yml
spring:
application:
name: eureka-server
server:
port: 8100
eureka:
instance:
hostname: 127.0.0.1
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8200/eureka/
fetch-registry: true
register-with-eureka: true
eureka-server02的application.yml
spring:
application:
name: eureka-server
server:
port: 8200
eureka:
instance:
hostname: 127.0.0.1
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8100/eureka/
fetch-registry: true
register-with-eureka: true
在這裡要注意一點:兩個eureka-server的名稱必須起的相同,並在注意在eureka-sever01的application.yml中的service-url配置的是eureka-server02的的地址,同理在eureka-sever02的application.yml中的service-url配置的是eureka-server01的的地址,這樣進行相互註冊。
啟動類:
@SpringBootApplication
@EnableEurekaServer
public class EurekasServer01Application {
public static void main(String[] args) {
SpringApplication.run(EurekasServer01Application.class, args);
}
}
@SpringBootApplication
@EnableEurekaServer
public class EurekasServer02Application {
public static void main(String[] args) {
SpringApplication.run(EurekasServer02Application.class, args);
}
}
將兩個eureka-server均啟動後,等待一段時間(同步資料),再檢視,如下:
這樣實現了註冊中心的高可用。
3、啟動服務提供者
3.1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3.2 application.yml
spring:
application:
name: app-producer
server:
port: 8001
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka/,http://localhost:8200/eureka/
register-with-eureka: true
fetch-registry: true
在defaultZone中註冊eureka叢集的地址。
3.3 業務類
@SpringBootApplication
@RestController
@EnableEurekaClient
public class ProducerApplication {
@RequestMapping("/getMember")
public String getMember() {
return "呼叫會員服務成功!";
}
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
為了方便,將controller與啟動類寫在了一起。
4、啟動服務消費者
4.1 pom.xml 與服務提供者相同
4.2 application.yml
spring:
application:
name: app-consumer
server:
port: 8002
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka/,http://localhost:8200/eureka/
fetch-registry: true
register-with-eureka: true
4.3 controller
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getOrder")
public String getOrder() {
//呼叫生產者的服務 PRC遠端服務呼叫
String serviceUrl = "http://app-producer/getMember";
//在註冊中心根據app-producer轉換為對應的IP地址,底層呼叫httpClient發起呼叫
String result = restTemplate.getForObject(serviceUrl, String.class);
return result;
}
}
4.4 啟動類
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
當服務提供者和服務消費者都啟動後,註冊中心叢集中會選舉其中之一當做主的註冊中心,而另一個當做備用。
如下,選擇的eureka-server02當做了主註冊中心,將服務優先註冊在eureka-server02上。
而eureka-sever01就被當做從註冊中心,上面只維持了和eureka-server02的連線。
這時訪問http://localhost:8002/getOrder,通過consumer遠端呼叫producer的服務:
5、模擬宕機情況
現在我們來模擬主機eureka-server02宕機的情況,看會發生什麼?
現在將eureka-server02停掉:
經過一段時間,eureka-sever01就從備變為主,並且將原來eureka-server02上面的資料全部同步了過來:
再次訪問:
因此:雖然有兩個註冊中心,但在使用的時候是主從方式,先選擇一個主註冊中心儲存資料,當主宕機時,將資料會同步到從伺服器,(需要一會兒時間才能完成同步)。
專案原始碼:https://gitee.com/liuzhoujian/springcloud-eureka-cluster