Spring Cloud學習筆記26——Spring Cloud 微服務實戰
阿新 • • 發佈:2018-12-15
微服務構建:Spring Boot
構建Maven專案
- 通過官方的
Spring Initializr
工具來產生基礎專案。
- 下載並解壓生成的專案壓縮包,並用
IDE
以Maven
專案匯入,以Intellij IDEA
為例。
- 單擊
Import project from external model
並選擇Maven
,一直單擊Next
按鈕。
實現RESTful API
在Spring Boot
中建立一個RESTful API的實現程式碼同Spring MVC
應用一樣,只是不需要像Spring MVC
那樣先做很多配置,而是像下面這樣直接開始編寫Controller
- 新建
package
:com.study.springcloud.hello.controller
。
- 新建
HelloController
類,內容如下所示。
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(){
return "Hello World";
}
}
- 啟動該應用,通過瀏覽器訪問
http;//localhost:8080/hello
。
引入actuator
在pom.xml
的dependency
spring-boot-starter-actuator
的依賴,如下。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
重新啟動應用,有一批端點定義,這些端點並非我們自己在程式中建立的,而是由spring-boot-starter-actuator
模組根據應用依賴和配置自動創建出來的監控和管理端點。通過這些端點,我們可以實時獲取應用的各項監控指標。這些內容將幫助我們制定更為個性化的監控策略。
服務治理:Spring Cloud Eureka
搭建服務註冊中心
- 首先,建立一個基礎的
Spring Boot
工程,命名為eureka-server
,並在pom.xml
中引入必要的依賴內容,pom.xml
完整程式碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
<!--Eureka Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--通用測試模組,包含JUnit、Hamcrest、Mockito-->
<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.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshot</name>
<url>https://repo.spring.io/snapshot</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Milestone</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>aliyun</id>
<name>Aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
- 通過
@EnableEurekaServer
註解啟動一個服務註冊中心提供給其他應用進行對話。只需在一個普通的Spring Boot
應用中新增這個註解即可:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 在預設設定下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行為,在
application.properties
中增加如下配置:
#指定服務註冊中心的埠,與後續要進行註冊的服務區分
server.port=1111
#例項的主機名稱
eureka.instance.hostname=localhost
#由於該應用為註冊中心,所以設定為false,代表不向註冊中心註冊自己
eureka.client.register-with-eureka=false
#由於註冊中心的職責就是維護服務例項,它並不需要去檢索服務,所以也設定為false
eureka.client.fetch-registry=false
#服務的URL
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
- 啟動應用並訪問
http://localhost:1111/
,可以看到如下頁面,其中Instances currently registered with Eureka
欄是空的,說明該註冊中心還沒有註冊任何服務。
註冊服務提供者
修改前面的Spring Boot
入門專案,將其作為一個微服務應用向服務註冊中心釋出自己。
- 首先,修改
pom.xml
,增加Spring Cloud Eureka
模組的依賴,具體程式碼如下所示:
<dependencies>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--全棧Web開發模組,包含嵌入式Tomcat、Spring MVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--通用測試模組,包含JUnit、Hamcrest、Mockito-->
<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.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshot</name>
<url>https://repo.spring.io/snapshot</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Milestone</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>aliyun</id>
<name>Aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
- 接著,改造
/hello
請求處理介面,通過注入DiscoveryClient
物件,在日誌中打印出服務的相關內容。
@RestController
public class HelloController {
private final Logger logger= Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance=client.getLocalServiceInstance();
logger.info("hello, host:"+instance.getHost()+", service_id:"+instance.getServiceId());
return "Hello World";
}
}
- 然後,在主類中通過加上
@EnableDiscoveryClient
註解,啟用Eureka
中的DiscoveryClient
實現(自動化配置,建立DiscoveryClient
介面針對Eureka
客戶端的EurekaDiscoveryClient
例項),才能實現上述Controller
中對服務資訊的輸出。
@EnableDiscoveryClient
@SpringBootApplication
public class HelloEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(HelloEurekaClientApplication.class, args);
}
}
- 最後,我們需要在
application.properties
配置檔案中,通過spring.application.name
屬性來為服務命名。再通過eureka.client.service-url.defaultZone
屬性來指定服務註冊中心的地址,這裡我們指定為之前構建的服務註冊中心地址,完整配置如下所示:
#為服務命名
spring.application.name=hello-eureka-client
#指定服務註冊中心的地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
- 分別啟動服務註冊中心以及這裡改造後的
hello-eureka-client
服務。訪問Eureka
的資訊面板。
- 通過訪問
http://localhost:8080/hello
,直接向該服務發起請求,在控制檯中可以看到如下輸出:
這些輸出內容就是之前我們在HelloController
中注入的DiscoveryClient
介面物件,從服務註冊中心獲取的服務相關資訊。
高可用註冊中心
在前面的服務註冊中心的基礎之上進行擴充套件,構建一個雙節點的服務註冊中心叢集。
- 建立
application-peer1.properties
,作為peer1
服務中心的配置,並將serviceUrl
指向peer2
:
#為服務命名
spring.application.name=eureka-server
#指定服務註冊中心的埠,與後續要進行註冊的服務區分
server.port=1111
#例項的主機名稱
eureka.instance.hostname=peer1
spring.profiles.active=peer1
#相互註冊要開啟
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
#服務的URL
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
- 建立
application-peer2.properties
,作為peer2
服務中心的配置,並將serviceUrl
指向peer1
:
#為服務命名
spring.application.name=eureka-server
#指定服務註冊中心的埠,與後續要進行註冊的服務區分
server.port=1112
#例項的主機名稱
eureka.instance.hostname=peer2
spring.profiles.active=peer2
#相互註冊要開啟
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
#服務的URL
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
- 在
/etc/hosts
檔案中新增對peer1
和peer2
的轉換,讓上面配置的host
形式的serviceUrl
能在本地正確訪問到;Windows
系統路徑為:C:\Windows\System32\drivers\etc\hosts
。
127.0.0.1 peer1
127.0.0.1 peer2
- 通過
spring.profiles.active
屬性來分別啟動peer1
和peer2
,先用mvn install
命令將應用打包成jar
包,再通過以下命令來啟動應用:
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
- 訪問
peer1
的註冊中心http://peer1:1111/
,可以看到registered-replicas
中已經有peer2
節點的eureka-server
了,且節點在可用分片(available-replicas
)中:
- 同樣地,訪問
peer2
的註冊中心http://peer2:1112/
:
- 將
peer1
關閉,重新整理http://peer2:1112/
,可以看到peer1
的節點變為了不可用分片(unavailable-replicas
):
- 在設定了多節點的服務註冊中心之後,服務提供方還需要做一些簡單的配置才能將服務註冊到
Eureka Server
叢集中。以hello-eureka-client
為例,修改application.properties
配置檔案,將註冊中心指向前面搭建的peer1
和peer2
,如下所示:
#為服務命名
spring.application.name=hello-eureka-client
#指定服務註冊中心的地址
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/
- 啟動
hello-eureka-client
,訪問http://peer1:1111/
和http://peer2:1112/
,可以看到hello-eureka-client
服務同時被註冊到了peer1
和peer2
上。若此時斷開peer1
,由於hello-eureka-client
服務同時也向peer2
註冊,因此在peer2
上的其他服務依然能訪問到hello-eureka-client
服務,從而實現了服務註冊中心的高可用:
- 如不想使用主機名來定義註冊中心的地址,也可以使用
IP
地址的形式,但是需要在配置檔案中增加配置引數eureka.instance.prefer-ip-address=true
,該值預設為false
。
服務發現與消費
下面來嘗試構建一個服務消費者,它主要完成兩個目標,發現服務以及消費服務。其中,服務發現的任務由Eureka
的客戶端完成,而服務消費的任務由Ribbon
完成。
下面我們通過構建一個簡單的示例,看看在Eureka
的服務治理體系下如何實現服務的發現與消費。
- 首先,我們做一些準備工作。啟動之前實現的服務註冊中心
eureka-server
以及hello-eureka-client
服務,為了實驗Ribbon
的客戶端負載均衡功能,我們通過java -jar
命令列的方式來啟動兩個不同埠的hello-eureka-client
,具體如下:
java -jar hello-eureka-client-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar hello-eureka-client-0.0.1-SNAPSHOT.jar --server.port=8082
- 訪問
http://localhost:1111/
頁面:
- 建立一個
Spring Boot
的基礎工程來實現服務消費者,取名為ribbon-consumer
,並在pom.xml
中引入如下的依賴內容,即新增了Ribbon
模組的依賴spring-cloud-starter-ribbon
。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!--全棧Web開發模組,包含嵌入式Tomcat、Spring MVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--通用測試模組,包含JUnit、Hamcrest、Mockito-->
<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.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency