当前位置: 移动技术网 > IT编程>开发语言>Java > 从springboot到springcloud的Config配置介绍

从springboot到springcloud的Config配置介绍

2020年07月31日  | 移动技术网IT编程  | 我要评论
之前的一个个零碎的组件,如果一步步来的话,可能已经发现问题了----没有集中配置jar包版本。同理,是不是还缺一个集中文件配置?所以这章来使用config-server来集中管理所有组件配置。配置方式主要有两种:1. 配置在服务(或本地)中2. 配置在 git/svn中(本章以git为例)一、配置在本地中1.1 创建config-server服务1.2 修改配置文件spring.application.name=config-server# 应用...


之前的一个个零碎的组件,如果一步步来的话,可能已经发现问题了----没有集中配置jar包版本。

同理,是不是还缺一个集中文件配置?所以这章来使用config-server来集中管理所有组件配置。


配置方式主要有两种:

1. 配置在服务(或本地)中

2. 配置在 git/svn 中(本章以git为例)


一、配置在本地中

1.1 创建config-server服务

1.2 修改配置文件

spring.application.name=config-server

# 应用服务 WEB 访问端口
server.port=8888

#eureka注册服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

#取本地配置(默认从resources文件下读取配置文件)
spring.profiles.active=native
#读取指定文件
#spring.cloud.config.server.native.search-locations=classpath:/

1.3 在启动类上添加两个注解

@EnableEurekaClient
@EnableConfigServer
package com.example.configserver;

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);
    }

} 

1.4 创建两个公共配置文件 common-tset.properties、common-dev.properties 并添加配置信息

message = test
message = dev

1.5 修改eureka-client 服务(用来调用config-server的配置信息)

1.5.1 在eureka-client服务的pom文件中添加调用config-server的依赖jar包

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

1.5.2 在eureka-client服务中创建bootstrap.properties并添加调用config-server服务的配置信息

其配置信息格式如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

label: 文件路径

application:文件名

profile:配置策略(用于哪个环境下)

以 /{application}-{profile}.properties 为例

#读取config-server下的配置文件
spring.cloud.config.name=common
spring.cloud.config.profile=test

#开启服务配置
spring.cloud.config.enabled=true
#配置中心名称(可以是集群)
spring.cloud.config.discovery.service-id=config-server

1.6 先后启动 eureka-server、config-server、eureka-client 服务 进行测试


二、配置在 git 中

2.1 在父工程下新建common-test.properties配置文件 上传到 git

message = springcloud_properties_message

2.2 修改config-server 服务的配置文件(将...active改为...git.uri)即可

spring.application.name=config-server

# 应用服务 WEB 访问端口
server.port=8888

#eureka注册服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

#取本地配置(默认从resources文件下读取配置文件)
#spring.profiles.active=native
#读取指定文件
#spring.cloud.config.server.native.search-locations=file:D:/properties/

#取git的配置文件
spring.cloud.config.server.git.uri=https://github.com/houfanGitHub/springcloud/
#spring.cloud.config.server.git.search-paths=
#如果是私有项目,则需要配置账号密码,公共则不需要
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password= 

2.3 修改eureka-client服务的pom文件(添加config.label)

#取git地址--需要添加文件分支
spring.cloud.config.label=master

2.4 重启服务测试




注意:这样会有一个bug!!!

如果再将配置改为取本地common-test.properties的配置信息的时候 取的是父工程的同名文件,

如果取的是dev环境的配置信息,取的config-server中的配置信息。

解决办法:

使用相对定位的方式,指定配置文件的位置。

这段配置上面已经给出。



源码地址:https://github.com/houfanGitHub/springcloud.git



本文地址:https://blog.csdn.net/qq_38553950/article/details/107675204

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网