环境搭建
采用docker-compose搭建测试环境
# docker-compose参考:https://github.com/nacos-group/nacos-docker/blob/master/example/standalone-mysql-5.7.yaml
# Nacos文档:https://nacos.io/zh-cn/index.html
version: '3'# 网桥 -> 方便相互通讯
networks:nacos:driver: bridgeservices:nacos:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/nacos-server:2.2.0 # 原镜像`nacos/nacos-server:2.2.0`container_name: nacos_server # 容器名为'nacos_server'restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录- "./nacos/logs:/home/nacos/logs"environment: # 设置环境变量,相当于docker run命令中的-e- PREFER_HOST_MODE=hostname # 如果支持主机名可以使用hostname,否则使用ip,默认也是ip- MODE=standalone # 单机模式启动- SPRING_DATASOURCE_PLATFORM=mysql # 数据源平台 仅支持mysql或不保存empty# TODO 修改mysql连接信息- MYSQL_SERVICE_HOST=10.11.68.77 # 注:这里不能为`127.0.0.1`或`localhost`方式!!!- MYSQL_SERVICE_DB_NAME=nacos_config # 所需sql脚本位于 `nacos/nacos-mysql.sql`- MYSQL_SERVICE_PORT=3306- MYSQL_SERVICE_USER=root- MYSQL_SERVICE_PASSWORD=root# TODO 修改JVM调优参数- JVM_XMS=128m #-Xms default :2g- JVM_XMX=128m #-Xmx default :2g- JVM_XMN=64m #-Xmn default :1g- JVM_MS=32m #-XX:MetaspaceSize default :128m- JVM_MMS=32m #-XX:MaxMetaspaceSize default :320mports:- "8848:8848"depends_on:- mysqlnetworks:- nacosmem_limit: 1000m # 最大使用内存mysql:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7container_name: nacos-mysqlrestart: unless-stoppedvolumes:- "./nacos/mysql5.7/my.cnf:/etc/mysql/my.cnf"- "./nacos/mysql5.7/data:/var/lib/mysql"- "./nacos/mysql5.7/log/mysql/error.log:/var/log/mysql/error.log"environment:TZ: Asia/ShanghaiLANG: en_US.UTF-8MYSQL_ROOT_PASSWORD: root # 设置root用户密码MYSQL_DATABASE: nacos_config # 初始化数据库ports:- "3306:3306"networks:- nacos
启动环境
# tips: 先修改配置与导入`nacos/nacos-mysql.sql`
docker-compose -f docker-compose.yml -p nacos up -d
访问地址:ip地址:8848/nacos 登录账号密码默认:nacos/nacos
新增配置:
一、添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>nacos</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><!-- 2.1.2版本以上支持纯净版客户端 --><nacos.version>0.2.7</nacos.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>${nacos.version}</version></dependency></dependencies>
</project>
二、配置文件和启动类
nacos:config:server-addr: 127.0.0.1:8848
@SpringBootApplication
@NacosPropertySource(dataId = "test", autoRefreshed = true)// dataId同nacos中的id保持一致
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}
三、Controller
@Controller
@RequestMapping("config")
public class ConfigController{@NacosValue(value="${userLocalCache:false}",autoRefreshed = true)private boolean useLocalCache;@RequestMapping(value="/get",method=GET)@ResponseBodypublic boolean get(){return useLocalCache;}
}
测试:
访问 http://localhost:8080/config/get,返回内容是 false
通过nacos控制台修改配置:dataId 为example,内容为useLocalCache=true
再次访问 http://localhost:8080/config/get,此时返回内容为true,说明程序中的useLocalCache值已经被动态更新了。