Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设。施。# 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: bridge
services:
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 :320m
ports:
- "8848:8848"
depends_on:
- mysql
networks:
- nacos
mem_limit: 1000m # 最大使用内存
mysql:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7
container_name: nacos-mysql
restart: unless-stopped
volumes:
- "./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/Shanghai
LANG: en_US.UTF-8
MYSQL_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
server.port=8086
useLocalCache=false
<?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>
package com.et.nacos.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
@RequestMapping("config")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
return useLocalCache;
}
}
nacos:
config:
server-addr: 127.0.0.1:8848
package com.et.nacos;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@NacosPropertySource(dataId = "test", autoRefreshed = true)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
访问 http://localhost:8080/config/get
,返回内容是 false
。example
,内容为useLocalCache=true
http://localhost:8080/config/get
,此时返回内容为true
,说明程序中的useLocalCache
值已经被动态更新了。