首先得要有github,或者gitlab,itee的賬號
config配置中心
概述
Spring-Cloud-Config:
- 做項目, 那么就少不了配置 微服務架構中,配置文件眾多,各個服務的配置文件也有可能不一樣,
- Spring為我們提供了相應的配置中心組件--Spring Cloud config
- 他是一個配置管理中心,用於集中管理程序中各個環境下的配置,我們可以將配置通過git或svn等方式推送到我們的應用程序
- 同Eureka一樣,他也分為server端與client端
優點
- 提供 服務端 和 客戶端 支持
- 集中式 管理分布式環境下的應用配置
- 基於 Spring 環境,無縫 與 Spring 應用集成
- 可用於 任何 語言開發的程序
- 默認實現基於 git 倉庫,可以進行 版本管理
- 可替換 自定義實現
Spring Cloud Config Server 作為配置中心服務端
- 拉取配置時更新 git 倉庫副本,保證是最新結果
- 支持數據結構豐富,yml, json, properties 等
- 配合 eureke 可實現服務發現,配合 cloud bus 可實現配置推送更新
- 配置存儲基於 git 倉庫,可進行版本管理
- 簡單可靠,有豐富的配套方案
Spring Cloud Config Client 默認客戶端實現
- SpringBoot 項目不需要改動任何代碼 加入一個啟動配置文件指明使用
ConfigServer 上哪個配置文件即可
config-server服務端配置
工程搭建
- 創建一個config-server工程管理添加依賴
dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- 創建啟動類,添加注解
@SpringBootApplication
@EnableConfigServer
public class configApp {
public static void main(String[] args) {
SpringApplication.run(configApp.class, args);
}
}
- 上傳github的配置文件
新創建一個倉庫,克隆到本地
創建testConfigServer.yml添加如下配置
spring:
profiles:
active: dev
---
# 開發環境
spring:
profiles: dev
server:
port:
1000
---
#測試環境
spring:
profiles: stg
server:
port:
1001
把創建的復制到克隆的地方
提交並上傳到github倉庫
4. 創建application.yml核心配置文件
server:
port: 2000
spring:
application:
name: testConfigServer
cloud:
config:
server:
git: #倉庫地址
uri:
- 啟動訪問http://localhost:2000/testConfigServer-dev.yml
訪問規則:
/{application}/{profile}[/{label}] /{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
解釋:
application: 配置文件的名字
profile:對應的環境
label:不同的分支
- 如果配置文件放入了github倉庫中的某個目錄組需要添加以下配置
Config Client配置
1. 在user或goods工程中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2. 把user或goods的application配置文件上傳到github倉庫更改名稱防止文件重名
3. 在要使用配置文件的微服務當中添加一個bootstrap.yml的配置文件
spring:
cloud:
config:
name: goods #讀取github的goods配置文件
uri: http://localhost:2000/ #config server的地址
label: master #分支名稱
刪除原來的goods的application.yml
4.啟動Eureka服務和goods服務,如果能起啟動成功,並且注冊到了Eureka表示已經config配置完成
啟動成功,並且端口號是我們自己配置的端口
瀏覽器訪問:http://localhost:5001/getGoods.do
Config集群配置
1.新創建一個configserver工程,注意修改端口號
2. 在原有的configserver和新創建的configServer添加如下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. 在兩個configserver的啟動類添加@EnableEurekaClient注解
3. 在兩個工程的配置文件中添加
eureka:
client:
serviceUrl:
#eureka服務端提供的注冊地址 參考服務端配置的這個路徑
defaultZone: http://eureka:3000/eureka,http://eureka1:3001/eureka,http://eureka2:3002/eureka2
instance:
instance-id: config-server-0 #此實例注冊到eureka服務端的唯一的實例ID
prefer-ip-address: true #是否顯示IP地址
#eureka客戶需要多長時間發送心跳給eureka服務器,表明它仍然活着,默認為30 秒 (與下面配置的單位都是秒)
leaseRenewalIntervalInSeconds: 1
#Eureka服務器在接收到實例的最后一次發出的心跳后,需要等待多久才可以將此實例刪除,默認為90秒
leaseExpirationDurationInSeconds: 3
4. 在goods或者user工程中添加依賴
5. 在bootstrap.yml添加配置
spring:
cloud:
config:
name: user #這是我們要讀取的配置文件名 對應獲取規則的{application}
#profile: dev #這個是要獲取的環境 對應的便是{profile}
label: master #這個就是獲取的節點 對應的是{label}
discovery:
enabled: true
service-id: testConfigServer #client-server的名稱
eureka:
client:
serviceUrl:
defaultZone: http://eureka:3000/eureka,http://eureka1:3001/eureka,http://eureka2:3002/eureka2
6. 啟動configServerEureka和user或goods
瀏覽器訪問Eureka
Spring-Cloud-Netflix完結