1.创建父项目,引入依赖
<?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>
<packaging>pom</packaging>
<modules>
<module>cloud-cache</module>
<module>cloud-admin</module>
<module>cloud-config</module>
<module>cloud-gateway</module>
<module>cloud-commons</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<groupId>com.xw</groupId>
<artifactId>cloud_alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.1 引入springboot,使得当前项目成为boot项目,声明类型为pom
<packaging>pom</packaging> <!-- 声明项目类型为pom -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1.2通过dependencyManagement统一声明springCloud版本号
<dependencyManagement>
<dependencies>
<!-- springcloud对应的cloud-alibaba版本号 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Springcloud版本号 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
下面是SpringCloud对应的alibaba及springboot版本
2.实现nacos服务注册
注意:
nacos需要提前装在windows或服务器上,(https://github.com/alibaba/*nacos*/releases/tag/1.4.1),我这里提供的是windows1.4.1版本的下载地址
2.1引入nacos实现服务注册的依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.2在yml中配置nacos
server:
port: 8001
spring:
application:
name: cloud-cache
cloud:
nacos:
discovery:
server-addr: localhost:8848
redis:
database: 0
host: localhost
port: 6379
timeout: 3000
2.3定义启动类(Application)
/**
* @EnableDiscoveryClient 开启nacos注册服务
*/
@EnableDiscoveryClient
@SpringBootApplication
public class CloudCacheApplication {
public static void main(String[] args) {
SpringApplication.run(CloudCacheApplication.class,args);
}
}
2.4启动应用,访问nacos,观察注册的服务(http://ip:端口号/nacos)
注:此处的端口号指nacos服务的,默认即是8848