Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它爲基於JVM的雲應用開發中涉及的配置管理、服務發現、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分佈式會話和集羣狀態管理等操做提供了一種簡單的開發方式。web
Spring Cloud包含了多個子項目(針對分佈式系統中涉及的多個不一樣開源產品),好比:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項目。spring
「微服務架構」在這幾年很是的火熱,以致於關於微服務架構相關的開源產品被反覆的說起(好比:netflix、dubbo),Spring Cloud也因Spring社區的強大知名度和影響力也被廣大架構師與開發者備受關注。bash
那麼什麼是「微服務架構」呢?簡單的說,微服務架構就是將一個完整的應用從數據存儲開始垂直拆分紅多個不一樣的服務,每一個服務都能獨立部署、獨立維護、獨立擴展,服務與服務間經過諸如RESTful API的方式互相調用。架構
對於「微服務架構」,能夠閱讀始祖Martin Fowler的《Microservices》(中文版翻譯點擊查看)app
Spring Cloud Eureka是Spring Cloud Netflix項目下的服務治理模塊。而Spring Cloud Netflix項目是Spring Cloud的子項目之一,主要內容是對Netflix公司一系列開源產品的包裝,它爲Spring Boot應用提供了自配置的Netflix OSS整合。經過一些簡單的註解,開發者就能夠快速的在應用中配置一下經常使用模塊並構建龐大的分佈式系統。它主要提供的模塊包括:服務發現(Eureka),斷路器(Hystrix),智能路由(Zuul),客戶端負載均衡(Ribbon)等。負載均衡
下面,就來具體看看如何使用Spring Cloud Eureka實現服務治理。分佈式
建立一個基礎的Spring Boot工程,命名爲 eureka-server
,並在 pom.xml
中引入須要的依賴內容:spring-boot
<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應用中添加這個註解就能開啓此功能,好比下面的例子:微服務
@SpringBootApplication
@EnableEurekaServer
public class SpringclouddemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringclouddemoApplication.class, args);
}
}
複製代碼複製代碼
在默認設置下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲,只須要在 application.properties
配置文件中增長以下信息:工具
spring.application.name=eureka-server
server.port=1001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
複製代碼複製代碼
爲了與後續要進行註冊的服務區分,這裏將服務註冊中心的端口經過server.port屬性設置爲1001。啓動工程後,訪問: http://localhost:1001/ ,能夠看到下面的頁面,其中尚未發現任何服務。
下面咱們建立提供服務的客戶端,並向服務註冊中心註冊本身。
首先,建立一個基本的Spring Boot應用。命名爲 eureka-client
,在 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() {
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}
複製代碼複製代碼
最後在應用主類中經過加上 @EnableDiscoveryClient
註解,該註解能激活Eureka中的DiscoveryClient實現,這樣才能實現Controller中對服務信息的輸出。
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(
ComputeServiceApplication.class)
.web(true).run(args);
}
}
複製代碼複製代碼
咱們在完成了服務內容的實現以後,再繼續對 application.properties
作一些配置工做,具體以下:
經過 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