1、pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2、application.properties
# 应用名称
spring.application.name=provider2004
# 应用服务 WEB 访问端口
server.port=2004
# Nacos帮助文档:https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=http://127.0.0.1:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public
3、bootstrap.properties 该配置为必须
# Nacos帮助文档:https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
##spring.cloud.nacos.config.username=nacos
##spring.cloud.nacos.config.password=nacos
##spring.cloud.nacos.config.contextPath=/nacos
# 设置配置中心服务端地址
spring.cloud.nacos.config.server-addr=localhost:8848
# Nacos 配置中心的namespace。需要注意,如果使用 public 的 namcespace ,请不要填写这个值,直接留空即可
# spring.cloud.nacos.config.namespace=
spring.cloud.nacos.config.file-extension=yaml
##spring.cloud.nacos.config.group=DEFAULT_GROUP
注: 创建配置文件名为bootstrap.yml
,注意是bootstrap.xxx,而不是application
或者其他。原因如下
Nacos同springcloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application
具体原因请参考https://www.cnblogs.com/larscheng/p/11392466.html
4、Provider2004Application.java
Provider2004Application.java
@EnableDiscoveryClient
@RestController
@RefreshScope
@SpringBootApplication
@Api(value="hello provider2004")
public class Provider2004Application {
public static void main(String[] args) {
SpringApplication.run(Provider2004Application.class, args);
}
@ApiOperation(value = "/hello",notes = "query hello",response =String.class)
@RequestMapping(value = "/hello/{string}",method = RequestMethod.GET)
public String echo(@PathVariable String string, HttpServletRequest request){
return " from provider2004 Hello "+string+request.getRemoteAddr();
}
@Value("${provider2004.config}")
private String config;
@ApiOperation(value = "/config",notes = "query hello",response =String.class)
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String config(){
return " from provider2004 config "+config;
}
}
5、localhost:2004/config
参考:https://www.cnblogs.com/larscheng/p/11392466.html
6、bootstrap.properties增加nacos配置
i)、bootstrap.properties
# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
##spring.cloud.nacos.config.username=nacos
##spring.cloud.nacos.config.password=nacos
##spring.cloud.nacos.config.contextPath=/nacos
# 设置配置中心服务端地址
spring.cloud.nacos.config.server-addr=localhost:8848
# Nacos 配置中心的namespace。需要注意,如果使用 public 的 namcespace ,请不要填写这个值,直接留空即可
# spring.cloud.nacos.config.namespace=
spring.cloud.nacos.config.file-extension=yaml
##spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].data-id=provider2007.yaml
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].refresh=true
ii).Nacos
iii)、Provider2004Application.java
@EnableDiscoveryClient
@RestController
@RefreshScope
@SpringBootApplication
@Api(value="hello provider2004")
public class Provider2004Application {
public static void main(String[] args) {
SpringApplication.run(Provider2004Application.class, args);
}
@ApiOperation(value = "/hello",notes = "query hello",response =String.class)
@RequestMapping(value = "/hello/{string}",method = RequestMethod.GET)
public String echo(@PathVariable String string, HttpServletRequest request){
return " from provider2004 Hello "+string+request.getRemoteAddr();
}
@Value("${provider2004.config}")
private String config;
@Value("${provider2007.config}")
private String config2;
@ApiOperation(value = "/config",notes = "query hello",response =String.class)
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String config(){
return " from provider2004 config "+config+" from provider2007 config "+config2;
}
}
iiii)、