Eureka是一種基於REST(具像狀態傳輸)的服務,主要用於AWS雲中定位服務,以實現中間層服務器的負載平衡和故障轉移。本文記錄一個簡單的服務註冊與發現實例。html
GitHub地址:https://github.com/Netflix/eurekajava
服務註冊中心git
新建一個Maven項目,並刪除src文件夾,保留pom.xml ,做爲parent,固然也能夠不用github
在parent裏面新建一個springboot項目的Module,Eureka Serverspring
項目結構sql
maven引入jarapache
parent的 pom.xmlspringboot
是爲了統一版本服務器
<?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>cn.huanzi.qch</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 保留parent的pom.xml,統一jar的版本 --> <properties> <source.encoding>UTF-8</source.encoding> <java.version>1.8</java.version> <!-- 文件拷貝時的編碼 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 編譯時的編碼 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <mysql-connector-java.version>5.1.34</mysql-connector-java.version> <!-- activiti --> <activiti.version>5.22</activiti.version> <spring-boot.version>1.4.7.RELEASE</spring-boot.version> <!-- jasig cas --> <cas.server.version>3.4.3.1</cas.server.version> <cas.client.version>3.1.12</cas.client.version> <!-- Maven plugin --> <maven-source-plugin.version>2.0.3</maven-source-plugin.version> <maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version> <maven-assembly-plugin.version>2.2.1</maven-assembly-plugin.version> <maven-deploy-plugin.version>2.4</maven-deploy-plugin.version> <maven-war-plugin.version>2.1.1</maven-war-plugin.version> <maven-jar-plugin.version>2.3.2</maven-jar-plugin.version> <spring-cloud.version>Greenwich.RC1</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
eureka-server的 pom.xml
繼承parent
<?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>cn.huanzi.qch.eureka</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>eureka 註冊中心</description> <!--繼承信息--> <parent> <groupId>cn.huanzi.qch</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> </parent> <dependencies> <!-- eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- spring boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 構建工具 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件
server.port=1111 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
啓動類
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
啓動應用,訪問 http://localhost:1111/,註冊中心啓動成功,此時有0個服務
服務發現,能夠新建一個springboot項目,咱們直接使用以前寫的一個myspringboot項目
maven中引入相關jar
<!-- eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
若是沒有repositories還須要加入
<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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories>
配置文件加入註冊中心的地址,也就是Eureka-Server的配置文件裏面 eureka.client.serviceUrl.defaultZone
#eureka eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啓動類添加註解
@EnableEurekaClient @SpringBootApplication @RestController public class MyspringbootApplication{ public static void main(String[] args) { SpringApplication.run(MyspringbootApplication.class, args); } /** * 訪問首頁 */ @GetMapping("/index") public String index(){ return "hello springboot!"; } }
啓動客戶端服務
成功在註冊中心註冊成功,能夠對外提供服務
默認狀況下,Eureka使用客戶端心跳來肯定客戶端是否啓動。除非另有說明,不然發現客戶機不會根據Spring引導執行器傳播應用程序的當前健康檢查狀態。所以,在成功註冊後,Eureka老是宣佈應用程序處於「UP」狀態。能夠經過啓用Eureka健康檢查來更改此行爲,好比我如今將myspringboot服務停掉,但註冊中心依舊顯示爲UP,這樣就會形成我服務已經掛掉了,但註冊中心依然會認爲這個實例還活着。
Eureka-Client
#健康檢查(須要spring-boot-starter-actuator依賴) eureka.client.healthcheck.enabled=true # 續約更新時間間隔(單位秒,默認30秒) eureka.instance.lease-renewal-interval-in-seconds=10 # 續約到期時間(單位秒,默認90秒) eureka.instance.lease-expiration-duration-in-seconds=10
<!-- actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Eureka-Server
#設爲false,關閉自我保護 eureka.server.enable-self-preservation=false #清理間隔(單位毫秒,默認是60*1000) eureka.server.eviction-interval-timer-in-ms=10000
健康檢查,註冊中心將死去的服務剔除
Eureka-Server
一、引入的是spring-cloud-starter-netflix-eureka-server,使用的是@EnableEurekaServer
Eureka-Client
一、引入的是spring-cloud-starter-netflix-eureka-client,使用的是@EnableEurekaClient
代碼已經開源、託管到個人GitHub、碼雲: