JAVA全系列 教程
3762个小节阅读:7091.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
新建命名空间dev,在配置管理->配置列表里面切换dev环境,创建payment-dev.yaml文件。
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
xxxxxxxxxx
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentConfig
{
public static void main(String[] args)
{
SpringApplication.run(PaymentConfig3344.class,args);
log.info("******************** 支付服务启动成功 ***************");
}
}
创建配置文件名为bootstrap.yml
,注意是bootstrap.yml,而不是application
。
原因:
Nacos同SpringCloud-Config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。SpringBoot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application。
- bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
- application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
- bootstrap.yml 先于 application.yml 加载
xxxxxxxxxx
spring:
profiles:
#多环境配置
active: dev
application:
#项目名字
name: payment
cloud:
nacos:
config:
#服务器地址
server-addr: 192.168.47.100:8848
#默认为Public命名空间,可以省略不写 #对应建立的命名空间的UUID
namespace: 8e6ad638-30c9-490c-8b02-65be434ca3a2
#指定文件后缀
file-extension: yaml
#文件名 -- 如果没有配置则默认为 ${spring.appliction.name}
prefix: ${spring.application.name}
#指定配置群组 --如果是Public命名空间 则可以省略群组配置
group: DEFAULT_GROUP
注意:
spring.application.name
:可以看到必须可少的配置项spring.cloud.nacos.discovery.server-addr
:指定注册中心的地址,如果你不需要注册该服务,也可以去掉该项,并删除discovery依赖spring.cloud.nacos.config.server-addr
:指定配置中心的地址file-extension
:指定配置中心中配置文件的格式
上面的配置是为了保证服务的正常注册和配置获取,以及配置DataID
的正确性
xxxxxxxxxx
@RestController
public class HelloController {
@Value("${hello.message}")
private String hello;
@GetMapping("/hello")
public String hello(){
return hello;
}
}
出现报错,肯定配置写错了,检查5步曲
具体细节看下图