1.环境搭建
1.创建模块 sunrays-common-cloud-nacos-starter
2.目录结构
3.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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>sunrays-common-cloud-nacos-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
4.自动配置
1.NacosAutoConfiguration.java
package com.sunxiansheng.cloud.nacos.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@Slf4j
public class NacosAutoConfiguration {
@PostConstruct
public void logConfigSuccess() {
log.info("NacosAutoConfiguration has been loaded successfully!");
}
}
2.spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.cloud.nacos.config.NacosAutoConfiguration
5.引入cloud模块通用依赖
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
2.测试
1.创建模块 sunrays-common-cloud-nacos-starter-demo
2.目录结构
3.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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-demo</artifactId>
<version>2.0.0</version>
</parent>
<artifactId>sunrays-common-cloud-nacos-starter-demo</artifactId>
<dependencies>
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-nacos-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
4.application.yml
spring:
application:
name: sunrays-common-cloud-nacos-starter-demo
cloud:
nacos:
discovery:
enabled: true
server-addr:
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-nacos-starter-demo/logs
module: sunrays-demo/sunrays-common-cloud-nacos-starter-demo
5.NacosApplication.java
package com.sunxiansheng.cloud.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
6.启动测试,成功注册到Nacos