簡介
Spring Cloud是一個基于Spring Boot實現(xiàn)的云應(yīng)用開發(fā)工具笔链,它為基于JVM的云應(yīng)用開發(fā)中涉及的配置管理舰蟆、服務(wù)發(fā)現(xiàn)趣惠、斷路器狸棍、智能路由、微代理味悄、控制總線草戈、全局鎖、決策競選侍瑟、分布式會話和集群狀態(tài)管理等操作提供了一種簡單的開發(fā)方式猾瘸。
Spring Cloud包含了多個子項目(針對分布式系統(tǒng)中涉及的多個不同開源產(chǎn)品),比如:Spring Cloud Config丢习、Spring Cloud Netflix、Spring Cloud0 CloudFoundry淮悼、Spring Cloud AWS咐低、Spring Cloud Security、Spring Cloud Commons袜腥、Spring Cloud Zookeeper见擦、Spring Cloud CLI等項目。
本文示例代碼:eureka-demo
微服務(wù)架構(gòu)
“微服務(wù)架構(gòu)”在這幾年非常的火熱羹令,以至于關(guān)于微服務(wù)架構(gòu)相關(guān)的開源產(chǎn)品被反復(fù)的提及(比如:netflix鲤屡、dubbo),Spring Cloud也因Spring社區(qū)的強大知名度和影響力也被廣大架構(gòu)師與開發(fā)者備受關(guān)注福侈。
那么什么是“微服務(wù)架構(gòu)”呢酒来?簡單的說,微服務(wù)架構(gòu)就是將一個完整的應(yīng)用從數(shù)據(jù)存儲開始垂直拆分成多個不同的服務(wù)肪凛,每個服務(wù)都能獨立部署堰汉、獨立維護、獨立擴展伟墙,服務(wù)與服務(wù)間通過諸如RESTful API的方式互相調(diào)用翘鸭。
對于“微服務(wù)架構(gòu)”,大家在互聯(lián)網(wǎng)可以搜索到很多相關(guān)的介紹和研究文章來進行學習和了解戳葵。也可以閱讀始祖Martin Fowler的《Microservices》就乓,本文不做更多的介紹和描述。
服務(wù)治理
在簡單介紹了Spring Cloud和微服務(wù)架構(gòu)之后拱烁,下面回歸本文的主旨內(nèi)容生蚁,如何使用Spring Cloud來實現(xiàn)服務(wù)治理。
由于Spring Cloud為服務(wù)治理做了一層抽象接口邻梆,所以在Spring Cloud應(yīng)用中可以支持多種不同的服務(wù)治理框架守伸,比如:Netflix Eureka、Consul浦妄、Zookeeper尼摹。在Spring Cloud服務(wù)治理抽象層的作用下见芹,我們可以無縫地切換服務(wù)治理實現(xiàn),并且不影響任何其他的服務(wù)注冊蠢涝、服務(wù)發(fā)現(xiàn)玄呛、服務(wù)調(diào)用等邏輯。
所以和二,下面我們通過介紹Eureka服務(wù)治理尘喝。
Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix項目下的服務(wù)治理模塊敞恋。而Spring Cloud Netflix項目是Spring Cloud的子項目之一,主要內(nèi)容是對Netflix公司一系列開源產(chǎn)品的包裝,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合戴质。通過一些簡單的注解,開發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)橘原。它主要提供的模塊包括:服務(wù)發(fā)現(xiàn)(Eureka)利赋,斷路器(Hystrix),智能路由(Zuul)堡距,客戶端負載均衡(Ribbon)等甲锡。
下面,就來具體看看如何使用Spring Cloud Eureka實現(xiàn)服務(wù)治理羽戒。
創(chuàng)建“服務(wù)注冊中心”
創(chuàng)建一個基礎(chǔ)的Spring Boot工程缤沦,命名為eureka-server,并在pom.xml中引入需要的依賴內(nèi)容:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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注解啟動一個服務(wù)注冊中心提供給其他應(yīng)用進行對話易稠。這一步非常的簡單缸废,只需要在一個普通的Spring Boot應(yīng)用中添加這個注解就能開啟此功能,比如下面的例子:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
//SpringApplication.run(EurekaServerApplication.class, args);
new SpringApplicationBuilder(EurekaServerApplication.class).web(true).run(args);
}
}
在默認設(shè)置下驶社,該服務(wù)注冊中心也會將自己作為客戶端來嘗試注冊它自己呆奕,所以我們需要禁用它的客戶端注冊行為,只需要在application.properties配置文件中增加如下信息:
spring:
application:
name: eureka-server
server:
port: 8081
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
security:
basic:
enabled: true #開啟認證
user:
name: user
password: 123456
為了與后續(xù)要進行注冊的服務(wù)區(qū)分衬吆,這里將服務(wù)注冊中心的端口通過server.port屬性設(shè)置為8081梁钾。啟動工程后,訪問:http://localhost:8081/逊抡, 可以看到下面的頁面姆泻,其中還沒有發(fā)現(xiàn)任何服務(wù)。
創(chuàng)建“服務(wù)提供方”
下面我們創(chuàng)建提供服務(wù)的客戶端冒嫡,并向服務(wù)注冊中心注冊自己拇勃。本文我們主要介紹服務(wù)的注冊與發(fā)現(xiàn),所以我們不妨在服務(wù)提供方中嘗試著提供一個接口來獲取當前所有的服務(wù)信息孝凌。
首先方咆,創(chuàng)建一個基本的Spring Boot應(yīng)用。命名為eureka-client蟀架,在pom.xml中瓣赂,加入如下配置:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
<!-- 用于注冊中心訪問賬號認證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
其次榆骚,實現(xiàn)/test請求處理接口,通過DiscoveryClient對象煌集,在日志中打印出服務(wù)實例的相關(guān)內(nèi)容妓肢。
@RestController
public class ServiceSupporterController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/test")
public String test() {
return "hello world";
}
}
最后在應(yīng)用主類中通過加上@EnableDiscoveryClient注解,該注解能激活Eureka中的DiscoveryClient實現(xiàn)苫纤,這樣才能實現(xiàn)Controller中對服務(wù)信息的輸出碉钠。
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
}
}
我們在完成了服務(wù)內(nèi)容的實現(xiàn)之后,再繼續(xù)對application.properties做一些配置工作卷拘,具體如下:
spring:
application:
name: eureka-client
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://user:123456@localhost:8081/eureka/
instance:
prefer-ip-address: true
instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
通過spring.application.name
屬性喊废,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時候只需要使用該名稱就可以進行服務(wù)的訪問。eureka.client.serviceUrl.defaultZone
屬性對應(yīng)服務(wù)注冊中心的配置內(nèi)容栗弟,指定服務(wù)注冊中心的位置操禀。為了在本機上測試區(qū)分服務(wù)提供方和服務(wù)注冊中心,使用server.port
屬性設(shè)置不同的端口横腿。
啟動該工程后,再次訪問:http://localhost:8081/斤寂」⒑福可以如下圖內(nèi)容,我們定義的服務(wù)被成功注冊了遍搞。
當然罗侯,我們也可以通過直接訪問eureka-client服務(wù)提供的/dc接口來獲取當前的服務(wù)清單,只需要訪問:http://localhost:8082/dc溪猿,我們可以得到如下輸出返回:Hello world
本文參考了:
Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊與發(fā)現(xiàn)(Eureka钩杰、Consul)【Dalston版】