1.新建一个项目config-server
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.star.guo</groupId>
<artifactId>stage4</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- <groupId>com.star.guo</groupId> -->
<artifactId>config-server</artifactId>
<!-- <version>0.0.1-SNAPSHOT</version> -->
<name>config-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.添加注解
package com.star.guo;
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
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4添加和修改配置文件application.yml
server:
port: 8008
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: config-server
cloud:
config:
server:
git:
#git地址
uri: https://github.com/guoxingege/remote_config
#查找的路径
search-paths: properties
# default-label: master
#账号 git仓库为public,读取不需要账号密码
username:
#密码
password:
default-label: master
#对应的branch
label: master
#/{application}/{profile}[/{label}]
#/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.yml
#/{label}/{application}-{profile}.properties
#[label]是可以省略的,默认label是master分支
5.启动
先启动eureka再启动config-server
6.访问测试
访问http://localhost:8008/starguo/dev
会显示{"name":"starguo","profiles":["dev"],"label":null,"version":"1fefe71801d400e9f8fdf59db5e821f94e4055e2","state":null,"propertySources":[]}
*如果使用自己的git地址,记得把配置文件放上去就行