Spring Cloud 的 配置遠端服務中心簡單入門操作
阿新 • • 發佈:2019-02-17
1.配置 spring cloud 的遠端服務中心 ,需要先在Git上生成一個倉庫。在倉庫中配置 .properties 檔案
配置連線資料庫的四要素
2 .然後建立本地maven檔案 在pom.xml中配置 依賴的包
<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>cn.et</groupId> <artifactId>SpringCloud_ConfigServer</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 依賴springboot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 配置遠端中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
3.在maven專案的src/main/resources 建立一個application.properties檔案。在檔案中配置 倉庫的地址 和給倉庫配置一個埠 倉庫的地址字尾的.git 要去掉
spring.cloud.config.server.git.uri= https://github.com/zmw0221/SpringCloud
server.port=8089
4.最後 一個帶main方法的類用來啟動 服務
package cn.et; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; //必須新增SpringbootApplication 啟用Spring的自動配置功能 //要掃描新增@ComponentScan(掃描的包名) @SpringBootApplication /** * 啟用分散式配置中心 讀取遠端git倉庫獲取配置 * 所有spring的配置中心的配置檔案 都有以下幾種格式組成 * 1 配置檔案屬於哪個應用的(application) * 2 配置檔案屬於哪個 階段(profile) 開發階段 測試階段 產品階段 * 3 在git中的哪個分支(label) */ @EnableConfigServer public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } }
5.在瀏覽器中輸入路徑 看是否能訪問 遠端服務中心