在微服務中,服務註冊與發現對管理各個微服務子系統起着關鍵做用。隨着系統水平擴展的愈來愈多,系統拆分爲微服務的數量也會相應增長,那麼管理和獲取這些微服務的URL就會變得十分棘手,若是咱們每新加一個微服務,就要在其它用到此微服務的地方手動加上它的URL地址或者其餘通訊協議的地址,這樣會常常出錯,並且工做量巨大,一旦某個微服務的地址發生了變化,就要手動修改全部引用它的微服務的配置文件。因此spring-cloud eureka server就是爲了解決這樣的問題而出現,通過簡單的配置,便可自動註冊和發現微服務。java
Gitee碼雲git
上篇博客咱們介紹瞭如何搭建spring-cloud的配置中心,還有一個測試的web client去訪問它,此次咱們在以前的基礎上搭建一個eureka server,而且讀取配置中心的配置,而後把web client做爲Discovery Client註冊到eureka服務。首先在IntelliJ下新建一個Maven項目:web
而後在pom.xml中添加以下代碼:spring
<?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.zxuqian</groupId> <artifactId>registry</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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>Finchley.M9</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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
這裏用到了spring-cloud-starter-netflix-eureka-server
這個eureka-server核心依賴,還有訪問配置中心服務的客戶端組件spring-cloud-starter-config
。
而後在src/main/resources
下建立bootstrap.yml
文件,添加以下配置:apache
spring: application: name: eureka-server cloud: config: uri: http://localhost:8888
此文件配置了用以讀取配置文件的應用名,即spring.application.name
,它的名字對應於服務中心的文件名。另外Eureka的自動註冊和發現也是基於這個參數的。而後配置了配置服務中心的uri。
新建一個Java類,cn.zxuqian.Application
,使用以下代碼:json
package cn.zxuqian; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
這裏只用@EnableEurekaServer
一條註解即把該應用配置爲Eureka Server。而後在配置中心的git倉庫中建立eureka-server.yml
文件,添加以下配置:bootstrap
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false
此文件配置了eureka-server的端口,以及關閉eureka自我註冊和發現,由於若是不關閉的話,eureka在啓動過程當中就會去嘗試註冊本身,然而發現服務並無啓動就會報錯。到此,eureka server就配置完了。數組
如今咱們要更新web client,使之能夠被eureka自動註冊和發現。首頁在pom.xml中添加eureka client的依賴:瀏覽器
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
而後在Application
類中加上@EnableDiscoveryClient
註解:app
@EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
最後咱們建立一個類用以測試服務是否是已成功註冊和發現。新建一個Java類cn.zxuqian.controllers.ServiceInstanceController
,添加以下代碼:
package cn.zxuqian.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class ServiceInstanceController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}") public List<ServiceInstance> serviceInstancesByApplicationName( @PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } }
這是一個普通的RestController, 定義了一個DiscoveryClient
類型的變量並添加了@Autowire
註解。此註解是Spring框架提供的依賴注入功能,在Spring的context下,它會自動尋找DiscoveryClient
的實現類,此處是eureka client。關於Spring的一些特性和原理,將在之後的博文中講到。
此類還定義了serviceInstancesByApplicationName
方法,用以處理/service-instances/{applicationName}
請求。這裏的{applicationName}
匹配url路徑中/service-instances/
之後的部分,而後用@PathVariable
註解賦值給方法的applicationName
參數。例如訪問http://localhost:8080/service-instances/web-client
,那麼applicationName
的值就是web-client
。方法的做用是從discoveryClient中根據spring.application.name
的值來取出對應的實例信息,返回的是一個list,會自動轉換爲json數組的形式返回給瀏覽器。
由於eureka server和web client都須要從配置服務中讀取配置,因此先啓動config-server,而後再啓動eureka-server,最後啓動web-client,在啓動成功後可能須要稍等十幾秒讓eureka-server發現和註冊web-client。完成以後訪問http://localhost:8080/service-instances/web-client
,會獲得以下結果:
[{"host":"xuqians-imac","port":8080,"instanceInfo":{"instanceId":"xuqians-imac:web-client","app":"WEB-CLIENT","appGroupName":null,"ipAddr":"192.168.72.31","sid":"na","homePageUrl":"http://xuqians-imac:8080/","statusPageUrl":"http://xuqians-imac:8080/actuator/info","healthCheckUrl":"http://xuqians-imac:8080/actuator/health","secureHealthCheckUrl":null,"vipAddress":"web-client","secureVipAddress":"web-client","countryId":1,"dataCenterInfo":{"@class":"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name":"MyOwn"},"hostName":"xuqians-imac","status":"UP","leaseInfo":{"renewalIntervalInSecs":30,"durationInSecs":90,"registrationTimestamp":1525319124967,"lastRenewalTimestamp":1525319124967,"evictionTimestamp":0,"serviceUpTimestamp":1525319124363},"isCoordinatingDiscoveryServer":false,"metadata":{"management.port":"8080"},"lastUpdatedTimestamp":1525319124967,"lastDirtyTimestamp":1525319124297,"actionType":"ADDED","asgName":null,"overriddenStatus":"UNKNOWN"},"metadata":{"management.port":"8080"},"uri":"http://xuqians-imac:8080","serviceId":"WEB-CLIENT","secure":false,"scheme":null}]
注意一下,這裏並無把配置中心服務設置爲能夠被eureka server註冊和發現,由於這裏把配置文件都放到了config-server中,它和eureka server有着雞生蛋,蛋生雞的問題,因此若是要讓config-server被自動註冊和發現,那麼就須要單獨配置eureka server,而後在config server中配置eureka的uri,以及設置spring.cloud.config.discovery.enabled
爲true。具體之後須要用的時候再詳細說明。
配置eureka server至關簡單,只須要加一條@EnableEurekaServer
註解,並在配置中關閉自我註冊和發現便可。而後在客戶端應用的Application類中加上@EnableDiscoveryClient
註解便可。
歡迎訪問個人博客