这里使用码云环境,在码云创建一个仓库,名为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
扫描二维码关注公众号,回复:
9078218 查看本文章
就会获取到文件中的内容