微服務架構概念
微服務架構是一種架構概念,它的主要作用是將功能分解到離散的各個服務當中,從而降低系統的耦合性,並提供更加靈活的服務支持。
需要解決的問題:
- 客戶端如何訪問這些服務?
- 每個服務之間如何通信?
- 如此多的服務,如何管理?
- 服務掛了,如何解決?(備份方案,應急處理機制)
Spring Cloud Alibaba
是什么?
Spring Cloud Alibaba 致力於提供微服務開發的一站式解決方案。此項目包含開發分布式應用微服務的必需組件,方便開發者通過 Spring Cloud 編程模型輕松使用這些組件來開發分布式應用服務。
依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以將 Spring Cloud 應用接入阿里微服務解決方案,通過阿里中間件來迅速搭建分布式應用系統。
(地址:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md)
為什么?
2018 年 12 月 12 日,Netflix 宣布 Spring Cloud Netflix 系列技術棧進入維護模式(不再添加新特性)
同時
2018 年 10 月 31 日的凌晨,這個偉大的日子里,Spring Cloud Alibaba 正式入駐了 Spring Cloud 官方孵化器,並在 Maven 中央庫發布了第一個版本。
2019年8月1日,Spring Cloud Alibaba 發布第一個正式版本,順利完成孵化 在 Alibaba 倉庫發布第一個畢業版本
怎么做?
畢業前:
Spring Cloud Greenwich(對應springboot 2.1.x)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring Cloud Finchley(對應springboot 2.0.x)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
畢業后:
Spring Cloud Greenwich
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Spring Cloud Finchley
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
針對微服務需要解決的四個問題,解決方案
Nacos
是什么?
Nacos 致力於幫助您發現、配置和管理微服務。Nacos 提供了一組簡單易用的特性集,幫助您快速實現動態服務發現、服務配置、服務元數據及流量管理。
為什么?
解決那么多服務怎么治理?
怎么做?
環境:
- 64 bit OS,支持 Linux/Unix/Mac/Windows,推薦選用 Linux/Unix/Mac。
- 64 bit JDK 1.8+
- Maven 3.2.x+
下載並安裝
# 下載源碼
git clone https://github.com/alibaba/nacos.git
# 安裝到本地倉庫
cd nacos/
mvn -Prelease-nacos clean install -U
啟動
cd distribution/target/nacos-server-0.7.0/nacos/bin
# Linux
./startup.sh -m standalone
# Windows
startup.cmd
基於Docker安裝
git clone https://github.com/nacos-group/nacos-docker.git
cd nacos-docker
單機模式
docker-compose -f example/standalone-mysql.yaml up
集群模式
docker-compose -f example/cluster-hostname.yaml up
使用
- 添加服務發現依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- 添加nacos客戶端
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- bootstrap.properties
(Spring Boot 配置文件的加載順序,依次為 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml ,其中 bootstrap.properties 配置為最高優先級)
# 這里的應用名對應 Nacos Config 中的 Data ID,實際應用名稱以配置中心的配置為准
spring.application.name=nacos-provider-config
# 指定查找名為 nacos-provider-config.yaml 的配置文件
spring.cloud.nacos.config.file-extension=yaml
# Nacos Server 的地址
spring.cloud.nacos.config.server-addr=192.168.233.150:8848
- 配置的動態更新,增加一個 user.name 的屬性
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.233.150:8848
server:
port: 8082
management:
endpoints:
web:
exposure:
include: "*"
user:
name: caps
- 入口函數添加
@EnableDiscoveryClient
- 修改 Controller ,增加一個請求方法,測試配置更新效果
// 注入配置文件上下文
@Autowired
private ConfigurableApplicationContext applicationContext;
// 從上下文中讀取配置
@GetMapping(value = "/hi")
public String sayHi() {
return "Hello " + applicationContext.getEnvironment().getProperty("user.name");
}