SpringCloud是什么,安装Nacos配置中心应该怎么做
1. Nacos介绍
官网说明:Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
2. docker安装Nacos
基于liunx centos7,镜像nacos/nacos-server:v2.1.0
2.1 docker-compose.yaml
version: '3.7'
services:
nacos01:
image: nacos/nacos-server:v2.1.0
container_name: nacos01
# restart: always
ports:
- "8848:8848"
environment:
- TZ=Asia/Shanghai
- MODE=standalone
- JVM_XMS=128m
- JVM_XMX=512M
networks:
- my-net
networks:
#新增的网络 内部服务名调用
my-net:
external: true
如果需要配置本地数据库的可以参考这篇文章
2.2 启动后访问控制台
访问http://192.168.0.221:8848/nacos/,账号密码都是nacos
添加一个orderservice-dev.yaml配置,内容如下。
接下来在springboot中集成Nacos
3.Springboot集成Nacos
3.1 pom依赖
父依赖
parent>
groupId>
org.springframework.boot/groupId>
artifactId>
spring-boot-starter-parent/artifactId>
version>
2.6.8/version>
/parent>
springcloud版本管理包
dependencyManagement>
dependencies>
dependency>
groupId>
org.springframework.cloud/groupId>
artifactId>
spring-cloud-dependencies/artifactId>
version>
2021.0.3/version>
type>
pom/type>
scope>
import/scope>
/dependency>
/dependencies>
/dependencyManagement>
nacos配置依赖
!--引入cloud config-->
dependency>
groupId>
org.springframework.cloud/groupId>
artifactId>
spring-cloud-starter-config/artifactId>
/dependency>
!--引入nacos config依赖-->
dependency>
groupId>
com.alibaba.cloud/groupId>
artifactId>
spring-cloud-starter-alibaba-nacos-config/artifactId>
version>
2021.0.1.0/version>
/dependency>
!--cloud2021.x版本后 不在支持bootstrap No spring.config.import set-->
dependency>
groupId>
org.springframework.cloud/groupId>
artifactId>
spring-cloud-starter-bootstrap/artifactId>
/dependency>
一定要注意依赖,不然很多问题
3.2 yaml配置
特别注意加载配置文件的顺序 bootstrap ->
application,所以不要在application.yml写任何配置,不存在最好。新建一个bootstrap.yml
spring:
application:
#这个名词需要和nacos配置的一致
name: orderservice
profiles:
#指定环境
active: dev
cloud:
#不使用默认的config 使用nacos
config:
enabled: false
nacos:
config:
server-addr: 192.168.0.221:8848
file-extension: yaml
接下来从启动日志看下读取配置的规则:
Ignore the empty nacos configuration and get it based on dataId[orderservice] &
group[DEFAULT_GROUP]
Ignore the empty nacos configuration and get it based on dataId[orderservice.yaml] &
group[DEFAULT_GROUP]
Located property source: [BootstrapPropertySource {
name='bootstrapProperties-orderservice-dev.yaml,DEFAULT_GROUP'}
, BootstrapPropertySource {
name='bootstrapProperties-orderservice.yaml,DEFAULT_GROUP'}
, BootstrapPropertySource {
name='bootstrapProperties-orderservice,DEFAULT_GROUP'}
]
- 服务名+配置的文件后缀(默认
properties后缀)最优先,orderservice.${ file-extension} - 根据环境配置,
orderservice-${ spring.profiles.active} .${ file-extension}
3.3 测试配置动态化
//开启配置动态刷新
@RefreshScope
@Slf4j
@RestController
public class OrderController {
@Value("${
hello.world}
")
private String hello;
@GetMapping("/test")
public String test() throws Exception {
log.info("config -- {
}
", hello);
return hello;
}
}
3.4 测试日志
c.e.order.controller.OrderController : config -- 1345671234567887654
//修改为jack再次访问
c.e.order.controller.OrderController : config -- hello my name is jack
通过以上内容的阐述,相信大家对“SpringCloud是什么,安装Nacos配置中心应该怎么做”已经有了进一步的了解,更多相关的问题,欢迎关注网络或到官网咨询客服。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: SpringCloud是什么,安装Nacos配置中心应该怎么做
本文地址: https://pptw.com/jishu/652391.html
