使用开源中国的仓库进行配置
先创建 Config-service 配置服务

- 引入依赖
<!--config 服务中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--config eureka服务提供方 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- application.yml配置文件
#服务器名字
spring:
application:
name: config-server
#git配置
cloud:
config:
server:
git:
uri: https://gitee.com/feijiaqi/config-colud
username: *******@163.com
password: *******
#超时时间
timeout: 5
#分支
default-label: master
#服务器端口
server:
port: 9100
#指定注册中心地址
eureka:
client:
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
# register-with-eureka: false
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
# fetch-registry: false
#设置Eureka的地址
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- ConfigServiceApplication 启动类
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 ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
开源中国配置
- 创建config-colud仓库;
-
默认在master分支创建配置
- 创建product-service-test.yml文件
server:
port: 8771
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#服务器名字
spring:
application:
name: product-service
env: test #环境
branch: test #分支
/{label}/{}-
product-service-test.yml
-| name: product-service 配置文件的名字
访问方式
(一定要注意语法,如果有问题,会出错)
多种访问路径,可以通过启动日志去查看
例子 http://localhost:9100/product-service.yml
- /{name}-{profiles}.properties
- /{name}-{profiles}.yml
- /{name}-{profiles}.json
- /{label}/{name}-{profiles}.yml
name 对用配置文件服务器名称
profile 环境名称,开发、测试、生产
lable 仓库分支、默认master分支
product-service服务的配置文件
加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
修改product-service服务的配置文件,把application.yml 改为 bootstrap.yml
Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap,另外一种是 application,下面列举这两种配置文件的区别
加载顺序
若application.yml 和bootstrap.yml 在同一目录下:bootstrap.yml 先加载 application.yml后加载
bootstrap.yml 用于应用程序上下文的引导阶段。bootstrap.yml 由父Spring ApplicationContext加载。
product-service服务的配置文件
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: product-service
#指定从哪个配置中心读取
cloud:
config:
discovery:
service-id: CONFIG-SERVER
enabled: true
profile: test
#建议用lable去区分环境,默认是lable是master分支
label: master
访问 配置服务器查看http://localhost:9100/master/product-service-test.yml

网友评论