1. Eureka簡介
Eureka是一個用於服務註冊和發現的元件,分為Eureka Server和Eureka Client,Eureka Server為Eureka服務註冊中心,Eureka Client為Eureka客戶端。
Eureka是SpringCloud首選推薦的註冊與服務發現元件,與SpringCloud其他元件可以無縫對接。
Eureka的基本框架主要包括3種角色:
◊ Register Service:服務註冊中心,是一個Eureka Server,提供服務註冊與發現功能;
◊ Provider Service:服務提供者,是一個Eureka Client,提供服務;
◊ Consumer Service:服務消費者,是一個Eureka Client,消費服務。
服務消費基本過程:
(1)首先需要一個服務註冊中心Eureka Server;
(2)服務提供者Eureka Client向服務註冊中心Eureka Server註冊,將自己的資訊(服務名、服務IP地址)通過REST API的形式提交註冊週年更新Eureka Server;
(3)服務消費者Eureka Client向服務註冊中心Eureka Server註冊,服務消費者獲取服務註冊列表資訊。該列表資訊包含所有向服務註冊中心Eureka Server註冊的服務資訊;
(4)服務消費者Eureka Client獲取服務註冊列表資訊之後,獲得服務提供者的IP地址,通過HTTP遠端排程來消費服務提供者的服務。
2. Eureka示例專案
專案結構:採用Maven多Module結構
主Maven專案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>libing</groupId> <artifactId>libing-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>libing-eureka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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>Finchley.M9</spring-cloud.version> </properties> <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> <modules> <module>eureka-server</module> <module>eureka-client</module> </modules> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2.1 Eureka Server
Eureka Server依賴項spring-cloud-starter-netflix-eureka-server
<?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>priv.libing</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>libing</groupId> <artifactId>libing-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
application.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
fetch-registry: false # 禁用向自己註冊
register-with-eureka: false # 只維護例項,不去檢索服務
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
注:預設Eureka Server會向自己註冊
EurekaServerApplication.java:
package priv.libing.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
其中:註解@EnableEurekaServer開啟Eureka Server功能。
啟動程式,在瀏覽器中訪問Eureka主介面 http://localhost:8761/
2.2 Eureka Client
Eureka Client引用項:
◊ spring-cloud-starter-netflix-eureka-client
◊ spring-boot-starter-web
<?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>priv.libing</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>libing</groupId> <artifactId>libing-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <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-web</artifactId> </dependency> </dependencies> </project>
application.yml:
eureka: client: service-url: default-zone: http://locahost:8761/eureka/ server: port: 8762 spring: application: name: eureka-client
EurekaClientApplication.java:
package priv.libing.eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
其中,註解@EnableEurekaClient開啟Eureka Client功能。
啟動執行eureka-server及eureka-client:
以上示例程式碼:libing-eureka.zip
3. Eureka Server叢集
在實際專案中,微服務例項較多,需要對Eureka Server進行高可用叢集。
本地搭建Eureka Server叢集,修改C:\Windows\System32\drivers\etc\hosts
127.0.0.1 slave1 127.0.0.1 slave2
eureka-server調整結構:
application.yml:
spring: application: name: eureka-server
application-slave1.yml:
server: port: 8761 eureka: instance: hostname: slave1 client: service-url: default-zone: http://slave2:8762/eureka/
application-slave2.yml:
server: port: 8762 eureka: instance: hostname: slave2 client: service-url: default-zone: http://slave1:8761/eureka/
eureka-client專案application.yml:
eureka: client: service-url: default-zone: http://slave1:8761/eureka/, http://slave2:8762/eureka/ server: port: 8763 spring: application: name: eureka-client
(1)eureka-server執行:mvn:package
java -jar F:\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave1
java -jar F:\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=slave2
(2)eureka-server執行:IDEA配置一個專案多例項
Single instance only:取消勾選
Active profiles:slave1或slave2,分別配置,執行兩個例項。
以上示例程式碼:libing-eureka-slave.zip