SrpingCloud 之SrpingCloud config分散式配置中心搭建
阿新 • • 發佈:2018-11-15
1、搭建git環境 目的:持久化儲存配置檔案資訊
採用碼雲
建立後
繼續建立資料夾 用來區分不同的專案
下面就是git上存放配置檔案了。環境的區分 dev sit pre prd 開發 測試 預釋出 準生產
sit 和 prd 環境
ConfigServer環境搭建:
注意Config server也需要註冊到註冊中心的 client通過別名去讀取 而不是實際地址哦
分別去對應:
###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka spring: application: ####註冊中心應用名稱 name: config-server cloud: config: server: git: ###git環境地址 uri: https://gitee.com/toov5/distributed_configuration_file.git ####目錄檔案(區分專案的) search-paths: - Test ####讀取分支環境 label: master ####服務埠號 server: port: 8888
啟動類:
package com.toov5; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableConfigServerpublic class AppConfigServer { public static void main(String[] args) { SpringApplication.run(AppConfigServer.class, args); } }
開啟Eureka 開啟 ConfigServer
下面驗證配置檔案:
把配置檔案存放在git
在git環境上建立配置檔案命名規範
服務名稱-配置名稱-環境
提交以後,啟動專案,訪問:
下面就是 搭建client 去使用配置檔案
pom:
<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.toov5</groupId> <artifactId>SpringCloud-Configclient</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <!-- SpringBoot整合eureka客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
bootstrap.yml (注意一定要utf-8格式的!)
spring: application: ####註冊中心應用名稱 git上的字首 name: test-configClient cloud: config: ####讀取字尾 prd是對應的字尾 profile: prd ####讀取config-server註冊地址 confit-server的註冊名稱 別名 discovery: service-id: config-server enabled: true ##### eureka服務註冊地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka server: port: 8882
controller
package com.toov5.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Value("${motto}") //配置的key private String motto; @RequestMapping("/getMotto") public String getMotto() { return motto; } }
啟動
package com.toov5.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AppConfigClient { public static void main(String[] args) { SpringApplication.run(AppConfigClient.class, args); } }
bootstrap.yml檔案一定要 utf-8格式的! LZ在寫次博文時候就遇到了這個問題 折騰了一會才解決的!