一,下载Nacos
下载地址:https://github.com/alibaba/nacos/releases
二,启动Nacos
安装Nacos的bin目录下,
执行:startup.cmd -m standalone
然后打开上图红框的地址
三,配置服务
1 配置Nacos
创建命名空间(无论本地还是测试服务期,全部注册到Nacos上,通过命名空间可以区分本地还是测试服务器的服务)
2 创建一个配置文件
test.yml:
记得选择 YAML
spring: datasource: url: jdbc:mysql://localhost:3306/test1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root name: hello wrold!!!
四,编写项目
1 项目结构
一个父的pom,一个子项目
父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>3.0.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>nacos_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>nacos_test</name> <description>nacos_test</description> <modules> <module>test1</module> </modules> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Web依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 日志依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- Lombok工具 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> <!-- Actuator可以帮助你监控和管理Spring Boot应用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.0.2</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--Spring Cloud Alibaba微服务组件的依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2022.0.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2 子项目(test1)
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.test</groupId> <artifactId>nacos_test</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>test1</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.4.1</version> </dependency> <!-- pojo持久化使用 --> <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.2.0</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 引入alibaba-nacos-config依赖,可以从Nacos配置中心获得配置信息 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>4.0.4</version> </dependency> </dependencies> </project>
resource下,创建bootstrap.yml(不是application.yml)
这个yml会找到nacos中,我们前面创建的test.yml
server: port: 3000 spring: application: name: test1 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 #注册中心地址 namespace: local_test #命名空间 config: file-extension: yaml #文件后缀,只支持 properties 和 yaml prefix: test #文件名 namespace: local_test #命名空间 auto-refresh: true enable-remote-sync-config: true #启用远程同步配置 timeout: 3000 group: DEFAULT_GROUP #配置组 refresh-enabled: true server-addr: 127.0.0.1:8848 #配置中心地址
启动项:
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients @EnableDiscoveryClient public class Main { public static void main(String[] args) { SpringApplication.run(Main.class,args); } }
API:
package com.test.api; import com.alibaba.nacos.api.model.v2.Result; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("api/test") @RefreshScope public class ApiTestController { @Value("${name}") private String name; @RequestMapping(value = "test1", method = RequestMethod.GET) @ResponseBody public Result test1(){ return Result.success(name); } }
五,测试
访问:http://localhost:3000/api/test/test1
结果:{"code":0,"message":"success","data":"hello wrold!!!"}
修改nacos中test.yml (无需重启服务)
name: hello!!!
结果:{"code":0,"message":"success","data":"hello!!!"}
标签:Spring,boot,Nacos,springframework,cloud,spring,test,org,Cloud From: https://www.cnblogs.com/hanjun0612/p/17867349.html