springboot intergrated nacos quick start demo

HBLOG
2 min readFeb 27, 2024

--

一、nacos introduction

Nacos /nɑ:kəʊs/ is the abbreviation of Dynamic Naming and Configuration Service, a dynamic service discovery, configuration management and service management platform that makes it easier to build cloud native applications.

Nacos is dedicated to helping you discover, configure and manage microservices. Nacos provides a simple and easy-to-use feature set to help you quickly implement dynamic service discovery, service configuration, service metadata and traffic management.

Nacos helps you build, deliver and manage microservices platforms more agilely and easily. Nacos is the service infrastructure for building modern application architecture centered on “service” (such as microservice paradigm, cloud native paradigm)

二、environment setup

use docker-compose to build the test environment。the specific configuration as follows:

# Nacos document: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
container_name: nacos_server
restart: unless-stopped
volumes:
- "./nacos/logs:/home/nacos/logs"
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
# TODO modify mysql connection infomation
- MYSQL_SERVICE_HOST=10.11.68.77
- MYSQL_SERVICE_DB_NAME=nacos_config
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=root
# TODO modify JVM param
- 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
MYSQL_DATABASE: nacos_config
ports:
- "3306:3306"
networks:
- nacos

start test environment

# tips: import `nacos/nacos-mysql.sql`
docker-compose -f docker-compose.yml -p nacos up -d

access url :ip:8848/nacos default username/password:nacos/nacos

add configuration dataId named “test”

server.port=8086
useLocalCache=false

三、Code Project

pom.xml

<?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>
<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>

controller

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;
}
}

Application.yaml

nacos:
config:
server-addr: 127.0.0.1:8848

DemoApplication.java

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);
}
}

Code Repository

四、test

  1. open browser and input http://localhost:8080/config/get,response content isfalse
  2. you can modify conguration by nacos console,for example : modify:dataId change example,content changeuseLocalCache=true
  3. again access http://localhost:8080/config/get,response content is true,prove thatuseLocalCachevalue is updated 。

五、reference

--

--

HBLOG
HBLOG

Written by HBLOG

talk is cheap ,show me your code

No responses yet