廢話不多說直接上代碼
首先創(chuàng)建一個(gè)spring boot maven項(xiàng)目
-
pom文件增加依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>nacosDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>nacosDemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>image.gif -
在application.properties文件中添加配置
spring.application.name=agent spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 //定義配置文件后綴為.properties spring.cloud.nacos.config.file-extension=properties //此處為自定義命名空間,下面會(huì)有說明 spring.cloud.nacos.config.namespace=29de6443-9f76-4c4b-9ff2-f754961365d4 //此處為自定義分組,下面有說明 spring.cloud.nacos.config.group=OLD_GROUPimage.gif -
在啟動(dòng)類里面加上
@EnableDiscoveryClient -
在配置類上面添加實(shí)現(xiàn)當(dāng)前類屬性動(dòng)態(tài)更新
@RefreshScopepackage com.example.nacosdemo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RefreshScope @RestController @EnableDiscoveryClient public class NacosDemoApplication { public static void main(String[] args) { SpringApplication.run(NacosDemoApplication.class, args); } @Value("${redis.host}") private String config; @RequestMapping("/getValue") public String getValue() { return config; } }image.gif 官網(wǎng)下載nacos-server,啟動(dòng)nacos
-
創(chuàng)建命名空間imageimage.gif
-
創(chuàng)建dataid,配填寫配置imageimage.gif
完成以上操作即可順利啟動(dòng)




