安装及试用
https://github.com/alibaba/nacos/releases
放到指定目录
tar -xvf nacos.tar.gz
启动
sh startup.sh -m standalone
用户名 nacos,密码 nacos
[http://10.202.xx.xx:8848/nacos](http://10.202.xx.xx:8848/nacos)
springboot + nacos集成
动态配置管理
新建配置
Data Id : example
Group:DEFAULT_GROUP
配置内容:validate.on=true
依赖
spring + nacos开启登录403 unknow user, 需要升级1.2.1,
参考https://github.com/nacos-group/nacos-spring-project/issues/209
以及
// Springboot
compile group: 'com.alibaba.boot', name: 'nacos-config-spring-boot-starter', version: '0.2.6'
//spring
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.3.6</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
code
nacos.config.server-addr=ip:8848
nacos.config.namespace=xxx
@SpringBootApplication
@NacosPropertySource(dataId = "example", groupId="dev",autoRefreshed = true)
public class BrokerApplication {
public static void main(String[] args) {
SpringApplication.run(BrokerApplication.class, args);
}
}
注入: 类似value ,
//@Value("${validate.on}")
@NacosValue(value = "${validate.on:true}", autoRefreshed = true)
private Boolean validateOn;
private Set<String> rangeSet;// 注入集合场景
@NacosValue(value = "${range_set}", autoRefreshed = true)
public void setRange(String source){
rangeSet = Arrays.stream(source.split(",")).collect(Collectors.toSet()); ;
}
public Set<String> getRangeSet(){
return rangeSet;
}
//
// private Boolean validateOn;
可能你觉得配置放缓存中也解决了,缓存中需要每次读一下,这可自动刷入啊
服务发现
创建服务列表
服务名 : example
保护阈值:1
元数据:
注册服务到nacos
curl -X PUT '10.202..xx.xx:8848/nacos/v1/ns/instance?serviceName=example&ip=127.0.0.1&port=8080'
依赖
compile group: 'com.alibaba.boot', name: 'nacos-discovery-spring-boot-starter', version: '0.2.6'
查询
@Controller
@RequestMapping("/discovery")
public class NacosDiscoverControlelr {
@NacosInjected
private NamingService namingService;
@RequestMapping(value = "/get")
@ResponseBody
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
}
查看结果http://localhost:8080/discovery/get?serviceName=example
[
{
"instanceId":"127.0.0.1#8080#DEFAULT#DEFAULT_GROUP@@example",
"ip":"127.0.0.1",
"port":8080,
"weight":1,
"healthy":true,
"enabled":true,
"ephemeral":true,
"clusterName":"DEFAULT",
"serviceName":"DEFAULT_GROUP@@example",
"metadata":{
},
"instanceHeartBeatInterval":5000,
"instanceHeartBeatTimeOut":15000,
"ipDeleteTimeout":30000,
"instanceIdGenerator":"simple"
}
]
网友评论