文章目录
-
- 1.环境搭建
-
-
- [1.创建模块 sunrays-common-cloud-nacos-starter](#1.创建模块 sunrays-common-cloud-nacos-starter)
- 2.目录结构
- 3.pom.xml
- 4.自动配置
- 5.引入cloud模块通用依赖
-
- 2.测试
-
-
- [1.创建模块 sunrays-common-cloud-nacos-starter-demo](#1.创建模块 sunrays-common-cloud-nacos-starter-demo)
- 2.目录结构
- 3.pom.xml
- 4.application.yml
- 5.NacosApplication.java
- 6.启动测试,成功注册到Nacos
-
1.环境搭建
1.创建模块 sunrays-common-cloud-nacos-starter
2.目录结构
3.pom.xml
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>
<!-- Nacos的服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
4.自动配置
1.NacosAutoConfiguration.java
java
package com.sunxiansheng.cloud.nacos.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
* Description: Nacos自动配置
*
* @Author sun
* @Create 2025/1/7 16:41
* @Version 1.0
*/
@Configuration
@Slf4j
public class NacosAutoConfiguration {
/**
* 自动配置成功日志
*/
@PostConstruct
public void logConfigSuccess() {
log.info("NacosAutoConfiguration has been loaded successfully!");
}
}
2.spring.factories
java
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.cloud.nacos.config.NacosAutoConfiguration
5.引入cloud模块通用依赖
xml
<dependencies>
<!-- SLF4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- SpringBoot自动配置 -->
<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
<?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>
<!-- sunrays-common-cloud-nacos-starter -->
<dependency>
<groupId>com.sunxiansheng</groupId>
<artifactId>sunrays-common-cloud-nacos-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
4.application.yml
yaml
spring:
application:
name: sunrays-common-cloud-nacos-starter-demo # 服务名称
cloud:
nacos:
discovery:
enabled: true # 启用服务发现
server-addr: # Nacos地址
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-demo/sunrays-common-cloud-nacos-starter-demo/logs # 日志根目录(默认./logs)
module: sunrays-demo/sunrays-common-cloud-nacos-starter-demo # 模块根目录从仓库根目录开始(默认defaultModule)
5.NacosApplication.java
java
package com.sunxiansheng.cloud.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Description: Nacos启动类
*
* @Author sun
* @Create 2025/1/7 16:44
* @Version 1.0
*/
@SpringBootApplication
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}