- 作用
- 类型
- 存放位置优先级
- 切换生产环境
- 阿里巴巴Nacos
- 配置中心
帮助我们保存某个逻辑部分的参数文件,是因为在框架里面,某一部分的代码是需要参数化配置来进行赋值的,因为这样可以来使的代码更加的灵活,修改代码也变得灵活,如果不写配置文件的话修改代码就需要重新编译成class文件,如果写在配置文件里面就只需要修改配置文件里面的代码,不需要重新编译class文件
配置文件添加
输出
配置文件有两种类型
- 一种是以key=value键值对保存内容的properties
- 一种是以key:value保存的yml
两种类型基本上没什么区别
注意事项:
- 在yml文件中有些细节需要注意,冒号后面要空一格再写值,这是他的语法
- 当properties和yml同时存在的时候是会同时运行的,但是如果内容冲突的话那么properties的内容优先级是大于yml的
原因:
后执行会把前面的覆盖掉…
加载优先级
- 项目根目录下的config目录
- 项目根目录
- resources目录下的config目录
- resources
优先加载的配置文件会被后加载的配置文件覆盖掉,如果是出现内容一致冲突的情况下
在我们的项目开发当中会遇到几种生产环境: 生产环境(prod),开发环境(dev),测试环境(test)
那么遇到各种环境的情况下要更改源代码配置文件是很麻烦的所有springboot允许我们通过命名约定按照一定的格式(application-{profile}.properties/yml)来进行配置文件的切换,就不需要大幅度的更改原配置文件,可以让我们根据不同的环境来指定应用加载不同环境的参数,
配置文件的命名一定要按照springboot的约定来不然就会找不到 spring.profiles.active=xxx是更换配置文件的语法
例子:
原配置…
更换后
Nacos是什么?
一句话概括了Nacos 的作用,一个更易于构建云原生应用的服务动态发现(服务注册中心),配置管理和服务管理平台(配置中心) --------且开源(白嫖)----------
Nacos 可以作为服务注册中心、配置中心。
先不讲服务注册中心。
配置中心帮助我们管理配置文件
这个图已经讲的明明白白了,配置中心的作用:可通过配置中心修改配置文件
且修改之后会讲修改内容推送通知到服务
发布代表的是配置文件添加到了配置中心里面
道理还需动手去体会,下面是如何搭建一个配置中心的全过程细节…
先兄弟们打开github
然后翻到下面
下载他,下载之后解压打开nacos文件夹中的bin(用来存储可执行的二进制文件)
打开一定要对应着上面的方式打开,看你是用哪种方式我用的是windows 所以我是要先打开cmd 进入到当前目录然后在执行
startup.cmd -m standalone
打开报错jdk有问题的说你不是64位的或者不是1.8的或者以上的兄弟cdm里面 输出java -version看看你的配置
如果是一样的话还报错那一定就是你环境变量配置的有问题了,如果不是一样的jdk版本比我还低的话或者不是64位的话建议重新安装一个jdk
好如果没问题的话我们运行应该看到的就是
一个页面然后复制http的地址打开
8848的端口老是被占用了所以我就在conf目录下面的application.properties里面换了端口
nacos的服务端启动成功了 ,登录账号密码都是nacos
现在我们就去配置服务
在你要讲配置文件添加到配置中心的项目中加入这个依赖
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 2.1.4.RELEASE
如果弄完之后启动报错连接不到服务可以将这个依赖版本降一下
依赖添加好了之后在你的资源包里面找个位置添加一个bootstrap.properties的配置文件
生成之后他是会换图标的没换的看看是不是名字打错了(建议复制粘贴)
生成好了之后在bootstrap.properties里面中配置 Nacos server 的地址和应用名
spring.cloud.nacos.config.server-addr=127.0.0.1:8848//一定要记住你的端口哦,别换了端口这里不记得换 spring.application.name=example 这个是你的application的名字很重要!!! #之所以需要配置 spring.application.name ,是因为它是构成 Nacos 配置管理 dataId字段的一部分。
controller里面添加注解@RefreshScope实现配置自动更新
为了更方便的看到效果我建立了一个boot的实现类然后在调用输出查看更改内容的过程
package s838790springcloud.s83790cloudconfig.vo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@ConfigurationProperties(prefix = "book")
public class Book implements Serializable
{
private int id;
private String name;
private int price;
// @Value("${book.author}")
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
controller
package s838790springcloud.s83790cloudconfig.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import s838790springcloud.s83790cloudconfig.vo.Book;
@RestController
@RefreshScope
public class ConfigController
{
@Autowired
Book book;
@RequestMapping("/config")
public Object getConfig()
{
return book;
}
}
然后我们启动服务,去刚刚那个我们登录的网站
当然你也可以不用这个bootstrap.properties的这个配置文件可以用其他的配置文件,
你也可以在配置中心添加很多的配置只要他们的appname不一样就行
例子:
你现在有四个配置文件,
A,(appname=AAA)
B,(appname=BBB)
C,(appname=CCC)
D, (appname=DDD)
你可以在配置中心配置列表里面添加这四个的配置但是每次项目启动他只能有一个Appname 假如你现在启动四个配置文件全部开起来 然后优先级最后项目启动的appname是DDD那么配置中心管理的时候就只能管理这个DDD的配置文件
我现在启动的appname是CONFIGSERVICER所以我配置文件里面
现在只能对他进行编辑
才行配置中心才会正常推送,服务才会正常反应
2021-11-09 01:15:06.896 INFO 14256 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2021-11-09 01:20:06.911 INFO 14256 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2021-11-09 01:21:02.272 INFO 14256 --- [127.0.0.1_18874] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_18874] [polling-resp] config changed. dataId=CONFIGSERVICER.properties, group=DEFAULT_GROUP
2021-11-09 01:21:02.272 INFO 14256 --- [127.0.0.1_18874] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[CONFIGSERVICER.properties+DEFAULT_GROUP]
2021-11-09 01:21:02.280 INFO 14256 --- [127.0.0.1_18874] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_18874] [data-received] dataId=CONFIGSERVICER.properties, group=DEFAULT_GROUP, tenant=null, md5=8438a20d7bdddad2a103222a050e3b06, content=book.id=3000
book.name=java
book.price=200
book.author=200.state, type=properties
2021-11-09 01:21:02.280 INFO 14256 --- [127.0.0.1_18874] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_18874] [notify-context] dataId=CONFIGSERVICER.properties, group=DEFAULT_GROUP, md5=8438a20d7bdddad2a103222a050e3b06
2021-11-09 01:21:05.256 WARN 14256 --- [127.0.0.1_18874] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[CONFIGSERVICER] & group[DEFAULT_GROUP]
2021-11-09 01:21:05.261 INFO 14256 --- [127.0.0.1_18874] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-CONFIGSERVICER.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-CONFIGSERVICER,DEFAULT_GROUP'}]
2021-11-09 01:21:05.262 INFO 14256 --- [127.0.0.1_18874] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2021-11-09 01:21:05.269 INFO 14256 --- [127.0.0.1_18874] o.s.boot.SpringApplication : Started application in 2.987 seconds (JVM running for 743.706)
2021-11-09 01:21:05.360 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ...
2021-11-09 01:21:07.110 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Unregistering ...
2021-11-09 01:21:07.125 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIGSERVICER/DESKTOP-DQ6G6JT.lan:CONFIGSERVICER:11113 - deregister status: 200
2021-11-09 01:21:07.134 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient
2021-11-09 01:21:07.135 INFO 14256 --- [127.0.0.1_18874] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING
2021-11-09 01:21:07.135 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1
2021-11-09 01:21:07.138 INFO 14256 --- [127.0.0.1_18874] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson
2021-11-09 01:21:07.139 INFO 14256 --- [127.0.0.1_18874] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson
2021-11-09 01:21:07.139 INFO 14256 --- [127.0.0.1_18874] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml
2021-11-09 01:21:07.139 INFO 14256 --- [127.0.0.1_18874] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Disable delta property : false
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Single vip registry refresh property : null
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Force full registry fetch : false
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Application is null : false
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Registered Applications size is zero : true
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Application version is -1: true
2021-11-09 01:21:07.195 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
2021-11-09 01:21:07.204 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : The response status is 200
2021-11-09 01:21:07.205 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is: 30
2021-11-09 01:21:07.206 INFO 14256 --- [127.0.0.1_18874] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is 4
2021-11-09 01:21:07.207 INFO 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1636392067207 with initial instances count: 6
2021-11-09 01:21:07.210 INFO 14256 --- [127.0.0.1_18874] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application ConFIGSERVICER with eureka with status DOWN
2021-11-09 01:21:07.210 WARN 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1636392067210, current=DOWN, previous=STARTING]
2021-11-09 01:21:07.210 INFO 14256 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIGSERVICER/DESKTOP-DQ6G6JT.lan:CONFIGSERVICER:11113: registering service...
2021-11-09 01:21:07.210 INFO 14256 --- [127.0.0.1_18874] o.s.c.n.e.s.EurekaServiceRegistry : Registering application ConFIGSERVICER with eureka with status UP
2021-11-09 01:21:07.211 WARN 14256 --- [127.0.0.1_18874] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1636392067211, current=UP, previous=DOWN]
2021-11-09 01:21:07.212 INFO 14256 --- [127.0.0.1_18874] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [book.author]
2021-11-09 01:21:07.212 INFO 14256 --- [127.0.0.1_18874] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_18874] [notify-ok] dataId=CONFIGSERVICER.properties, group=DEFAULT_GROUP, md5=8438a20d7bdddad2a103222a050e3b06, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@4d6306e6
2021-11-09 01:21:07.212 INFO 14256 --- [127.0.0.1_18874] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_18874] [notify-listener] time cost=4932ms in ClientWorker, dataId=CONFIGSERVICER.properties, group=DEFAULT_GROUP, md5=8438a20d7bdddad2a103222a050e3b06, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@4d6306e6
2021-11-09 01:21:07.217 INFO 14256 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIGSERVICER/DESKTOP-DQ6G6JT.lan:CONFIGSERVICER:11113 - registration status: 204
2021-11-09 01:21:07.217 INFO 14256 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIGSERVICER/DESKTOP-DQ6G6JT.lan:CONFIGSERVICER:11113: registering service...
2021-11-09 01:21:07.231 INFO 14256 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CONFIGSERVICER/DESKTOP-DQ6G6JT.lan:CONFIGSERVICER:11113 - registration status: 204
刷新一下
这些是我自己玩出来的经验,如有不对请评论区指点一下,感激不尽



