這里我們會用到Spring Cloud Netflix,該項目是Spring Cloud的子項目之一,主要內容是對Netflix公司一系列開源產品的包裝,它為Spring Boot應用提供了自配置的Netflix OSS整合。通過一些簡單的注解,開發者就可以快速的在應用中配置一下常用模塊並構建龐大的分布式系統。它主要提供的模塊包括:服務發現(Eureka),斷路器(Hystrix),智能路有(Zuul),客戶端負載均衡(Ribbon)等。
所以,我們這里的核心內容就是服務發現模塊:Eureka。下面我們動手來做一些嘗試。
創建“服務注冊中心”
創建一個基礎的Spring Boot工程,並在pom.xml
中引入需要的依賴內容:
<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> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
通過@EnableEurekaServer
注解啟動一個服務注冊中心提供給其他應用進行對話。這一步非常的簡單,只需要在一個普通的Spring Boot應用中添加這個注解就能開啟此功能,比如下面的例子:
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
在默認設置下,該服務注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為,只需要在application.properties
中問增加如下配置:
server.port=1111 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
為了與后續要進行注冊的服務區分,這里將服務注冊中心的端口通過server.port
屬性設置為1111
。
啟動工程后,訪問:http://localhost:1111/
可以看到頁面,其中還沒有發現任何服務
創建“服務提供方”
下面我們創建提供服務的客戶端,並向服務注冊中心注冊自己。
假設我們有一個提供計算功能的微服務模塊,我們實現一個RESTful API,通過傳入兩個參數a和b,最后返回a + b的結果。
首先,創建一個基本的Spring Boot應用,在pom.xml
中,加入如下配置
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</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.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
其次,實現/add
請求處理接口,通過DiscoveryClient
對象,在日志中打印出服務實例的相關內容。
@RestController
public class ComputeController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return r;
}
}
最后在主類中通過加上@EnableDiscoveryClient
注解,該注解能激活Eureka中的DiscoveryClient
實現,才能實現Controller中對服務信息的輸出。
@EnableDiscoveryClient
@SpringBootApplication
public class ComputeServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
}
}
我們在完成了服務內容的實現之后,再繼續對application.properties
做一些配置工作,具體如下:
spring.application.name=compute-service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
通過spring.application.name
屬性,我們可以指定微服務的名稱后續在調用的時候只需要使用該名稱就可以進行服務的訪問。
eureka.client.serviceUrl.defaultZone
屬性對應服務注冊中心的配置內容,指定服務注冊中心的位置。
為了在本機上測試區分服務提供方和服務注冊中心,使用server.port
屬性設置不同的端口。
啟動該工程后,再次訪問:http://localhost:1111/
可以看到,我們定義的服務被注冊了。