這裏使用碼雲環境,在碼雲創建一個倉庫,名爲config_server。
進入倉庫,新建一個文件夾名爲springcloud_config。
進入文件夾,創建兩個配置文件,名爲test-configClient-prd.properties和test-configClient-sit.properties,
命名規範是服務名稱-環境名稱.properties或yml。
打開eclipse,創建一個maven項目,依賴引入
<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.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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>
application.properties配置文件
###服務註冊到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
spring:
application:
####註冊中心應用名稱
name: config-server
cloud:
config:
server:
git:
###git環境地址
uri: https://gitee.com/vhukze/config_server.git
####搜索目錄
search-paths:
- springcloud_config
####讀取分支
label: master
####端口號
server:
port: 8888
configServer服務需要放在註冊中心上面供其他服務讀取配置文件的,所以需要一個Eureka註冊中心,搭建Eureka註冊中心的步驟之前已經寫過了:搭建Eureka註冊中心
uri那裏填寫的是剛纔創建的倉庫的地址,search-paths填寫倉庫中創建的文件夾的名稱。
然後創建一個啓動類。
package com.vhukze.configServerApp;
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
@EnableConfigServer
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
啓動項目後訪問http://localhost:8888/test-configClient-prd.properties
就會獲取到文件中的內容