Nacos"拍了拍你",说配置中心应该这么玩儿
上一篇我们一起学习了 Nacos服务注册发现,负载均衡–SpringCloud Alibaba从入门到入土(四),今天我们一起学习:
Nacos做配置中心
依旧是不多哔哔,先上官网https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html 官网看明白的可以跳过第一节,懒得看的乖乖往下看 下面是几个关键点:
依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
DateId 格式:
${prefix}-${spring.profile.active}.${file-extension}
很多同学不知道dataid怎么配置,这里我带大家详细看一下文档
这是官网上的描述,应该是很清楚了;
prefix spring.application.name的值
spring.profile.active
file-extension
那么我的dataid 就应该是 nacos-config 加上 -dev 加上 .yaml
把nacos的连接配置放到bootstrap.yml,再从nacos中读取统一管理的配置
bootstrap.yml(bootstrap.properties)与application.yml(application.properties)执行顺序
bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
bootstrap.yml 加载优先级高于 application.yml
@RefreshScope 注解自动刷新配置
如何使用?
1. 第一种方式 @Value 直接获取
@RestController
@RefreshScope // nacos 动态刷新
public class ConfigController {
@Value("${nacos.study}")
private String info;
@GetMapping(value = "/getInfo")
public String getConfig(){
return info;
}
}
2. 第二种方式 调用API
官方API 提供了调用方式
分组方案
由于 namespace group dataid 的概念(不了解的往回翻Nacos-Namespace/Group/DataId的介绍–SpringCloud Alibaba从入门到入土(三)),我们可以轻易的通过三者设定适合自己项目的配置分组方案,我这里整理了几种方式,仅供参考,具体项目具体分析:
1.单体项目???
dataid 方案:
2. 单租户 方案一
3. 单租户 方案二
4. 多租户
以上应该很好理解,看不懂的回去翻翻概念(Nacos-Namespace/Group/DataId的介绍–SpringCloud Alibaba从入门到入土(三))
配置中心demo
一个超级简单的demo
pom
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml
bootstrap.yml
server:
port: 8010
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
username: nacos
password: nacos
namespace:
config:
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
username: nacos
password: nacos
namespace:
file-extension: yaml
aplication.yml
spring:
profiles:
active: dev
# active: test
application
两个注解
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
controller
@RestController
@RefreshScope // nacos 动态刷新
public class ConfigController {
@Value("${nacos.study}")
private String info;
@GetMapping(value = "/getInfo")
public String getConfig(){
return info;
}
}
dataid
test
API 贴出来大家看一看,写的很详细;
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.api.config;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
/**
* Config Service Interface
*
* @author Nacos
*/
public interface ConfigService {
/**
* Get config
*
* @param dataId dataId
* @param group group
* @param timeoutMs read timeout
* @return config value
* @throws NacosException NacosException
*/
String getConfig(String dataId, String group, long timeoutMs) throws NacosException;
/**
* Get config and register Listener
*
* If you want to pull it yourself when the program starts to get the configuration for the first time,
* and the registered Listener is used for future configuration updates, you can keep the original
* code unchanged, just add the system parameter: enableRemoteSyncConfig = "true" ( But there is network overhead);
* therefore we recommend that you use this interface directly
*
* @param dataId dataId
* @param group group
* @param timeoutMs read timeout
* @param listener {@link Listener}
* @return config value
* @throws NacosException NacosException
*/
String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException;
/**
* Add a listener to the configuration, after the server modified the
* configuration, the client will use the incoming listener callback.
* Recommended asynchronous processing, the application can implement the
* getExecutor method in the ManagerListener, provide a thread pool of
* execution. If provided, use the main thread callback, May block other
* configurations or be blocked by other configurations.
*
* @param dataId dataId
* @param group group
* @param listener listener
* @throws NacosException NacosException
*/
void addListener(String dataId, String group, Listener listener) throws NacosException;
/**
* Publish config.
*
* @param dataId dataId
* @param group group
* @param content content
* @return Whether publish
* @throws NacosException NacosException
*/
boolean publishConfig(String dataId, String group, String content) throws NacosException;
/**
* Remove config
*
* @param dataId dataId
* @param group group
* @return whether remove
* @throws NacosException NacosException
*/
boolean removeConfig(String dataId, String group) throws NacosException;
/**
* Remove listener
*
* @param dataId dataId
* @param group group
* @param listener listener
*/
void removeListener(String dataId, String group, Listener listener);
/**
* Get server status
*
* @return whether health
*/
String getServerStatus();
}
以上
码字辛苦,转载注明出处;
不足之处,没有解释到位的地方欢迎大家指正,关注我带你从0到1学会
SpringCloud Alibaba; 如果对你有帮助,请给我个赞👍
下一篇,我们一起学习,Nacos集群的搭建;