在微服務架構這樣的分佈式環境中,我們需要充分的考慮發生故障的情況,所以在生產環境中我們必須對各個組件進行高可用部署。Eureka Server 的設計一開始就考慮了這個問題,在Eureka的服務治理設計中,所有的結點,既是服務提供方,也是服務消費方,服務註冊中心也不例外。Eureka Server 的高可用實際上就是將自己作爲服務向其他服務註冊中心註冊自己。這樣就可以形成一組相互註冊的服務註冊中心,以實現服務清單的相互同步,達到高可用的效果。
具體實現方式:搭建一個雙節點的服務註冊中心集羣
如A ,B兩個註冊中心,在建立時分別註冊到本身,並相互註冊。
實現方法一:
環境爲Ubuntu IntellijIDE jdk1.8
步驟1:建立eureak-server註冊中心
點擊File ,New,Project,選擇左側的Spring Initializr,並配置好JDK版本(一定爲jdk1.8)點擊next,
默認不變,再點擊next,選擇右側的Spring Cloud Discovery,對應選擇左側的Eureka Server,點擊next,輸入項目名,點擊finish 。需要分別建立兩個工程。
不需要配置pom.xml
在節點A和B中配置@EnableEurekaServer。
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
分別在圖中的application.properties配置文件中進行如下配置
節點A
spring.application.name=eureka.server server.port=1111 eureka.instance.hostname=127.0.0.1 eureka.client.fetch-registry=true eureka.client.register-with-eureka=true eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1112/eureka/
節點B
spring.application.name=eureka.server server.port=1112 eureka.instance.hostname=127.0.0.1 eureka.client.fetch-registry=true eureka.client.register-with-eureka=true eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/
該配置允許服務註冊中心註冊自己,因爲該配置是默認的,在實際開發過程中可以不加。進行完該配置之後,我們分別訪問http://localhost:1111/和http://localhost:1112/,註冊中心已經相互註冊。
步驟2 建立服務提供方eureka-client,將該服務註冊到A節點
點擊File ,New,Project,選擇左側的Spring Initializr,並配置好JDK版本(一定爲jdk1.8)點擊next,
默認不變,再點擊next,選擇右側的Spring Cloud Discovery,對應選擇左側的Eureka Discovery Client。點擊next,輸入項目名,點擊finish 。
配置pom.xml,添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在執行main的java類中加入:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在DemoApplication的同級目錄下,新建一個java類
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
// private final Logger logger = Logger.getLogger(getClass());
@Autowired
// private DiscoveryClient client;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
// ServiceInstance instance = client.getLocalServiceInstance();
// logger.info("/hello, host:"+instance.getHost()+"service_id"+instance.getServiceId());
return "hello_world";
}
}
application.properties配置文件中進行如下配置
spring.application.name=client-service
server.port=8011
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
注意由於端口名的衝突,如果所有的工程均在一臺電腦上建立的,則需要保證端口的不一致性。
我們分別訪問http://localhost:1111/和http://localhost:1112/,可以看到,client-service同時註冊到了上述的兩個節點中,
若斷開某一個節點,client-service仍然可以向另一個節點註冊,
且client-service只註冊了一個節點,由於註冊節點之間相互註冊,所以另一個節點也會被client-service註冊。
從而實現了服務註冊中心的高可用。
實現方法二:
編程環境 ubuntu jdk1.8 apache-maven-3.5.4-bin.tar.gz
首先安裝jdk和maven,並配置環境變量,測試是否已成功安裝。
其次下載eureak服務註冊代碼
如有三臺服務器,則每臺服務器均進行上述操作。
對每臺服務器的application.yml文件進行如下配置
server:
port: 8761
eureka:
instance:
hostname: 192.168.1.4
# prefer-ip-address: true
client:
service-url:
defaultZone: http://192.168.1.7:8762/eureka/,http://192.168.1.3:8763/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enable-self-preservation: false
其中enable-self-preservation: false表示關閉自我保護模式
client:
service-url:
defaultZone: http://192.168.1.7:8762/eureka/,http://192.168.1.3:8763/eureka/
表示將該服務註冊中心,註冊到其他的兩個服務註冊中心,組成集羣。注意代碼中的階梯順序一定要有。
其他兩臺機器,將修改端口號,主機名及註冊中心的網址。
在ssh登錄到服務器,進行運行時,防止終端關閉,服務自動關閉,使用如下命令進行服務的開啓:
setsid mvn spring-boot:run