這段時間開始整理以前的SpringCloud實踐筆記,這裏感謝翟永超大佬的文章SpringCloud從入門到精通的指導。html
項目結構
web
注意:
一、SpringCloud與SpringBoot有比較嚴格的版本對應關係,使用以前請肯定好對應相關版本
二、若是沒有特殊須要,建議各子模塊使用各自獨立的依賴,而不一樣一使用父模塊的pom依賴。否則可能出現衝突問題
三、本篇文章主要介紹項目的搭建,關於Eureka服務的註冊與發現相關理論知識,可移步Eureka服務註冊與發現spring
首先先建立一個SpringBoot工程,再建立一個名爲eureka-server的子模塊工程,並在pom.xml中引入Eureka服務註冊中心所需依賴,以下:app
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
經過@EnableEurekaServer註解就能啓動一個服務註冊中心spring-boot
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
application.properties配置示例以下:微服務
spring.application.name=eureka-server server.port=6601 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
默認設置下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要經過
eureka.client.register-with-eureka=false
禁用它的客戶端註冊行爲fetch
啓動工程後,訪問:http://localhost:6601/ ,可看到頁面以下圖所示:spa
註冊中心就開始運行了,能夠看到目前尚未微服務實例註冊3d
下面咱們建立提供服務的客戶端,並向服務註冊中心註冊本身。本文咱們主要介紹服務的註冊與發現,因此咱們不妨在服務提供方中嘗試着提供一個接口來獲取當前全部的服務信息。日誌
首先,建立一個命名爲eureka-clientSpring Boot應用,結構以下:
pom.xml以下配置:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
其次,實現/dc請求處理接口,經過DiscoveryClient對象,在日誌中打印出服務實例的相關內容。
@RestController public class DcController { @Autowired DiscoveryClient discoveryClient; @GetMapping("/dc") public String dc() throws InterruptedException{ // Thread.sleep(5000L); String services = "Services: " + discoveryClient.getServices(); System.out.println(services); return services; } }
應用主類中經過加上@EnableDiscoveryClient註解,該註解能激活Eureka中的DiscoveryClient實現,這樣才能實現Controller中對服務信息的輸出。
咱們在完成了服務內容的實現以後,再繼續對application.properties作一些配置工做,具體以下:
spring.application.name=eureka-client server.port=6602 eureka.client.serviceUrl.defaultZone=http://localhost:6601/eureka/
啓動該工程後,再次訪問:http://localhost:6601/。 能夠看到咱們定義的服務被成功註冊了。
固然,咱們也能夠經過直接訪問eureka-client服務提供的/dc接口來獲取當前的服務清單,只須要訪問:http://localhost:6602/dc, 咱們能夠獲得以下輸出返回:
Services: [eureka-client]