参考: https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
版本
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<springboot.version>2.5.3</springboot.version>
<lombok.version>1.18.10</lombok.version>
<liteflow.version>2.11.3</liteflow.version>
<nacos.config.version>0.2.12</nacos.config.version>
<nacos.discovery.version>0.2.12</nacos.discovery.version>
<mariadb.version>2.3.0</mariadb.version>
</properties>
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>web-parent</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos2</artifactId>
<name>nacos2</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
</dependencies>
<build>
<finalName>Nacos2</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yaml
spring:
application:
name: Nacos2
server:
servlet:
context-path: /nacos2
port: 8890
nacos:
#配置中心
config:
bootstrap:
enable: true
server-addr: 127.0.0.1:8848
namespace: f1f6eec6-1380-4731-8676-6e51ddeea861
group: web-project
data-id: web-nacos
type: yaml
auto-refresh: true
remote-first: true
# 服务注册与发现
discovery:
server-addr: 127.0.0.1:8848
#是否将本应用注册到nacos服务列表,默认是false
auto-register: true
#本服务所属的命名空间的ID,默认是空,也就是public
namespace: f1f6eec6-1380-4731-8676-6e51ddeea861
register:
#本服务是否接受外部的请求,默认true
enabled: true
#本服务要注册到命名空间下的哪个组,默认DEFAULT_GROUP
group-name: web-project
#服务的名称,会展示在nacos服务列表,要求唯一,可以不写,默认是spring.application.name的值
service-name: Nacos2
Nacos2App.java
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Nacos2App {
public static void main(String[] args) {
SpringApplication.run(Nacos2App.class, args);
}
}
TestController.java(测试配置中心)
package com.test.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/test")
@RestController
public class TestController {
@NacosValue(value = "${people.name}", autoRefreshed = true)
private String name;
@NacosValue(value = "${people.age}", autoRefreshed = true)
private int age;
@NacosValue(value = "${address}", autoRefreshed = true)
private String address;
@GetMapping("/get")
public String get() {
return name + ", "+ age +", "+ address;
}
}
DiscoveryController.java(测试服务注册发现)
调用获取实例方法时
@RequestMapping("/test2")
@RestController
public class DiscoveryController {
@NacosInjected
private NamingService namingService;
}