下面咱們建立提供服務的客戶端,並向服務註冊中心註冊本身。本文咱們主要介紹服務的註冊與發現,因此咱們不妨在服務提供方中嘗試着提供一個接口來獲取當前全部的服務信息.html
1. 首先咱們先建立一個SpringBoot 的項目,命名爲:erueka-client 在pom 中添加以下的配置:web
======================================================spring
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>bootstrap
<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>架構
=====================================================app
其次,實現/dc請求處理接口,經過DiscoveryClient對象,在日誌中打印出服務實例的相關內框架
@RestController public class DcController { @Autowired DiscoveryClient discoveryClient; @GetMapping("/dc") public String dc() { String services = "Services: " + discoveryClient.getServices(); System.out.println(services); return services; } }
最後:最後在應用主類中經過加上@EnableDiscoveryClient
註解,該註解能激活Eureka中的DiscoveryClient實現,這樣才能實現Controller中對服務信息的輸出。分佈式
代碼以下圖:spring-boot
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(
ClientApplication.class)
.web(true).run(args);
}
}
修改配置文件:application.properties ,具體修改內容以下:
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
經過spring.application.name
屬性,咱們能夠指定微服務的名稱後續在調用的時候只須要使用該名稱就能夠進行服務的訪問。eureka.client.serviceUrl.defaultZone
屬性對應服務註冊中心的配置內容,指定服務註冊中心的位置。爲了在本機上測試區分服務提供方和服務註冊中心,使用server.port
屬性設置不一樣的端口。
啓動該工程後,再次訪問:http://localhost:1001/。能夠以下圖內容,咱們定義的服務被成功註冊了。
固然,咱們也能夠經過直接訪問eureka-client
服務提供的/dc
接口來獲取當前的服務清單,只須要訪問:http://localhost:2001/dc,咱們能夠獲得以下輸出返回:
其中,方括號中的eureka-client
就是經過Spring Cloud定義的DiscoveryClient
接口在eureka的實現中獲取到的全部服務清單。因爲Spring Cloud在服務發現這一層作了很是好的抽象,因此,對於上面的程序,咱們能夠無縫的從eureka的服務治理體系切換到consul的服務治理體系中區。
Spring Cloud Consul項目是針對Consul的服務治理實現。Consul是一個分佈式高可用的系統,它包含多個組件,可是做爲一個總體,在微服務架構中爲咱們的基礎設施提供服務發現和服務配置的工具。它包含了下面幾個特性:
因爲Spring Cloud Consul項目的實現,咱們能夠輕鬆的將基於Spring Boot的微服務應用註冊到Consul上,並經過此實現微服務架構中的服務治理。
以以前實現的基於Eureka的示例(eureka-client)爲基礎,咱們如何將以前實現的服務提供者註冊到Consul上呢?方法很是簡單,咱們只須要在pom.xml
中將eureka的依賴修改成以下依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
接下來再修改一下application.properites
,將consul須要的配置信息加入便可,好比:(下面配置是默認值)
spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 |
到此爲止,咱們將eureka-client轉換爲基於consul服務治理的服務提供者就完成了。前文咱們已經有提到過服務發現的接口DiscoveryClient
是Spring Cloud對服務治理作的一層抽象,因此能夠屏蔽Eureka和Consul服務治理的實現細節,咱們的程序不須要作任何改變,只須要引入不一樣的服務治理依賴,並配置相關的配置屬性就能輕鬆的將微服務歸入Spring Cloud的各個服務治理框架中。
下面能夠嘗試讓consul的服務提供者運行起來。這裏可能讀者會問,不須要建立相似eureka-server的服務端嗎?因爲Consul自身提供了服務端,因此咱們不須要像以前實現Eureka的時候建立服務註冊中心,直接經過下載consul的服務端程序就可使用。
(
作服務發現的框架經常使用的有
)
到官網上面下載consul 服務器:
https://www.consul.io/downloads.html
環境變量配置:
在安裝的位置解壓獲得 consul.exe 文件(個人解壓位置是:D:\Program Files\consul)
環境變量增長一條:
增長一條D:\Program Files\consul
cmd 命令窗口執行:consul agent -dev
consul 自帶 UI 界面,打開網址:http://localhost:8500 ,能夠看到當前註冊的服務界面
cmd 命令窗口執行:consul.exe agent -server ui -bootstrap -client 0.0.0.0 -data-dir="E:\consul" -bind X.X.X.X
其中X.X.X.X爲服務器ip,便可使用http://X.X.X.X:8500 訪問ui而不是隻能使用localhost鏈接
具體啓動命令以下圖所示:
啓動以前得項目,進入界面訪問: