首页 技术 正文
技术 2022年11月16日
0 收藏 694 点赞 3,016 浏览 4900 个字

编程开发的时候有没有觉得很多配置文件需要维护,比如,修改了数据库连接,所有用到该数据库的服务配置都得替换,是不是超级的麻烦呢

下面,给大家介绍一下Spring提供的配置自动化组件-spring cloud config

config也就是配置的意思,springcloud也是想尽办法帮助大家节约开发和发布成本,这也是为什么大家喜欢用他的原因,并且这个配置中心也是极其的简单。

首先,所有的配置文件有两部分组成,第一:文件名   第二:所属的环境(即本地环境,测试环境或者线上环境,通常用dev,sit或者uat表示)

存储配置文件需要有个存储的地方,目前用的比较多的是数据库和远程仓库git

这次我已git为例,首先看下目录结构

SpringCloud之自动化配置-config

是不是非常的简单,一个目录下,就只有配置文件,文件格式命名: {profile}-{env}.properties

这里我推荐用properties后缀(因为我用yml文件试了很多次,都没有读取到数据,有机会再试试吧,大家知道的话也可以下方评论留言哈!)

该配置文件目录建好后,上传提交到git

SpringCloud之自动化配置-config

git地址为:

http://chengjq@192.168.129.200:8089/r/java/springConfig.git

新建配置中心服务config-server

还是先来看下目录结构,仍然是最简单的ConfigServerApplication.java , application.yml , pom.xml三个文件

SpringCloud之自动化配置-config

1.pom.xml

<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

config-server主要依赖于spring-cloud-starter-eureka-server,spring-cloud-starter-config这两个包

2.application.yml

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8888spring:
cloud:
config:
server:
git:
uri: http://chengjq@192.168.129.200:8089/r/java/springConfig.git
searchPaths: /**
username: chengjq
password: 123456
label: master application:
name: config-server

spring.cloud.config.server.git.uri:git的地址,即上面的  http://chengjq@192.168.129.200:8089/r/java/springConfig.git

searchpath:目录 /** 搜索所有

username:git的用户名

password:git的密码

当然,公开的git可以不填username和password

application.name:该应用的名字

3.ConfigServerApplication.java

@EnableEurekaServer
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
@EnableConfigServer:作为配置中心的标志

启动该项目,发现在eureka中已经注册成功,端口为配置文件中定义的8888

SpringCloud之自动化配置-config

ok,至此,配置中心已经搭建成功

下面,我们来写个例子,看下是否可以从git的配置文件中读取到该属性

配置客户端 config-client

首先,还是看下目录结构

SpringCloud之自动化配置-config

结构还是很简单,就这三个文件,ConfigClientApplication.java , application.yml , pom.xml

1.pom.xml

 <dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

想要使用配置中心,还是得有依赖:spring-cloud-starter-config

2. application.yml

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/spring:
cloud:
config:
name: ceshi
profile: dev
label: master
discovery:
enabled: true
service-id: config-server# uri: http://localhost:8888/ application:
name: config-clientserver:
port: 8089management:
security:
enabled: false

这块多了些参数

spring.cloud.config.name:文件名(我的文件名叫ceshi)

profile:适用的环境(我的是开发环境 简称dev)

label:分支(目前是master,即为主干)

uri:定义的config服务地址(我这里的是http://localhost:8888/,即为配置中心的地址)

discovery.enabled:服务发现(true or false )

discovery.service-id:服务名

正常情况下,uri和discovery.enabled,discovery.service-id任选其一就行,如果git会经常换地址,可以使用服务名

3.ConfigClientApplication.java

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope
public class ConfigClientApplication { @Value("${name}")
String name; @Value("${age}")
String age; public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
} @RequestMapping(value = "/hi")
public String hi(){
return "我是"+name+",今年"+age+"岁";
}
}

启动该客户端,看到应用已被注册成功

浏览器输入:http://localhost:8089/hi.发现获取到了数据

SpringCloud之自动化配置-config

至此,已经可以从配置中心获取到数据了

但是,大家会发现,如果我去修改配置文件信息,并且提交到git后,没法实施刷新页面信息,无法做到自动化配置

当然,springcloud还是提供了解决方案

看config-client中的配置文件,发现有个依赖:spring-boot-starter-actuator  该以来就是提供实时刷新,在需要获取配置数据的类上加上@RefreshScope

SpringCloud之自动化配置-config

然后在postman或者其他api接口调用工具上使用刷新接口即可,注意是post请求,这应该算是半自动化,如果想实现全自动化,可以实施监控git的push操作。

SpringCloud之自动化配置-config

再次去刷新地址,发现数据修改了

至此,半自动化的spring cloud config配置已完成,如有问题,请下方评论,谢谢!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900