- 根pom文件增加,spring-cloud-alibaba包管理,注意版本spring-boot 3.0.3,spring-cloud-alibaba 2022.0.0.0-RC1
<?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.1</version><relativePath/></parent><groupId>com.dashu</groupId><artifactId>technology</artifactId><version>0.0.1-SNAPSHOT</version><name>technology</name><description>technology</description><packaging>pom</packaging><properties><java.version>17</java.version><version>0.0.1-SNAPSHOT</version><alibaba-cloud.version>2022.0.0.0-RC1</alibaba-cloud.version></properties><modules><module>start</module><module>api</module><module>service</module><module>infrastructure</module></modules><dependencyManagement><dependencies><dependency><groupId>com.dashu</groupId><artifactId>api</artifactId><version>${version}</version></dependency><dependency><groupId>com.dashu</groupId><artifactId>infrastructure</artifactId><version>${version}</version></dependency><dependency><groupId>com.dashu</groupId><artifactId>service</artifactId><version>${version}</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${alibaba-cloud.version}</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>
- 在启动模块下加入依赖
<!--nacos配置中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!--nacos服务注册中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
- 如何安装nacos,直接参考官网就可以了https://nacos.io/docs/latest/quickstart/quick-start/
- application.yml配置文件
spring:application:name: technologycloud:nacos:discovery:server-addr: 127.0.0.1:8848config:server-addr: 127.0.0.1:8848file-extension: ymlconfig:import:- nacos:${spring.application.name}
-
到nacos配置中心增加一个配置项technology.yml
-
启动项目,写个获取配置文件参数的测试类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "sms")
@Data
public class SmsProperties {private String type;private String code;}
@RestController
@RequestMapping("/index")
public class indexController {@Autowiredprivate SmsProperties smsProperties;@GetMapping ("/nacos")public String nacosProperties(){return smsProperties.getType()+smsProperties.getCode();}
}
- 验证一下
修改配置,不重新启动程序,验证
- 服务管理,服务注册成功