轉自大神的文章:https://blog.csdn.net/tianyaleixiaowu/article/details/78184793
網上講這個東西的很多,抄來抄去的,大部分類似,多數沒講明白為什么那么配置。
譬如eureka.client.register-with-eureka和fetch-registry是否要配置,配不配區別在哪里;eureka的客戶端添加service-url時,是不是需要把所有的eureka的server地址都寫上,還是只需要寫一個server就可以了(因為server之間已經相互注冊了)?如果寫上了所有的server地址,那相當於將每個client服務都往所有的server都添加了一遍,那還配置server間的相互注冊有什么意義?
上面的這些問題在多數講eureka集群教程里都沒有說明白,上來就是配server相互注冊,client添加所有的server地址,大大的誤導了我一把。專門從頭新建了項目來看看到底eureka集群是該怎么配置。
server端配置
創建個eureka server項目
pom.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.tianyalei</groupId>
- <artifactId>eureka_server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>eureka_server</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <spring-cloud.version>Dalston.SR4</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- spring:
- application:
- name: eureka
- profiles:
- active: server1
添加了一個application-server1.yml
- server:
- port: 20001
- eureka:
- instance:
- hostname: server1
- client:
- # 表示是否注冊自身到eureka服務器
- # register-with-eureka: false
- # 是否從eureka上獲取注冊信息
- # fetch-registry: false
- service-url:
- defaultZone: http://server2:20002/eureka/
- server:
- port: 20002
- eureka:
- instance:
- hostname: server2
- client:
- #register-with-eureka: false
- #fetch-registry: false
- service-url:
- defaultZone: http://server1:20001/eureka/
instance.hostname是唯一標識。
由於我們使用了http://server1這種寫法,需要配一下host。Windows的host在/etc/host,mac的在/private/etc

然后在啟動類上加上EnableEurekaServer注解即可。
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaServerApplication.class, args);
- }
- }
在idea右上角run,選擇edit configrations


原本應該只有一個啟動項,點擊+號,給兩個啟動項都設置一下Program arguments,就是--spring.profiles.active分別設置為server1和server2,代表分別以兩個不同的配置來啟動項目。
然后把兩個啟動項都啟動起來,分別訪問各自的端口


可以看到圖上registered-replicas和available-replicas分別有了對方的地址。
eureka服務端的配置就這樣就OK了。
client端配置
新建一個eureka client項目。
pom如下
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.tianyalei</groupId>
- <artifactId>eureka_client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>eureka_client</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- <spring-cloud.version>Dalston.SR4</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- spring:
- application:
- name: eureka_client
- eureka:
- client:
- service-url:
- defaultZone: http://server1:20001/eureka/
在啟動類上加注解eurekaClient注解
然后啟動項目
- @SpringBootApplication
- @EnableEurekaClient
- public class EurekaClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaClientApplication.class, args);
- }
- }
再看看server端的界面


可以看到eureka_client已經在兩個server上都注冊上了,雖然我們在client的yml里default_zone只配置了server1。
其實這樣就已經達到了我們的目的,所有的客戶端都只指向一個eureka server地址,至於server端是怎么做高可用、怎么處理單體故障是客戶端不關心的。倘若client端配置了所有server的地址,那么每當server增加刪除了一個服務后,客戶端就需要隨之改變,那不是我們希望看到的。
測試很簡單,我們直接關掉server1,然后看看server2是否還能維持住client的發現。
關掉server1,20001的網頁已經打不開了,20002上也已經沒有了20001的服務發現,控制台在一直報錯。


但是我們看到client的注冊信息還在,這說明server2還能繼續提供注冊發現的服務,這樣就避免了單點故障后的整體服務發現的癱瘓。
下面我們可以測試一下把server端yml里配置register-with-eureka: false的那兩行注釋給放開,看看eureka的server忽略自己后,是否能完成服務發現的高可用。
測試很簡單,可以看到和上面的最終結果是一樣的,都是server1關閉后,server2依舊能進行client的發現。區別在於

這個就是和之前注釋掉后界面不同的地方。
至於在client端配置所有的server地址,各位可以自行嘗試。