关于Nacos集成SpringCloud依赖问题
最近在写Nacos集成SpringCloud的项目中经常遇到各种报错,经过仔细研究,发现POM文件中依赖存在冲突,解决办法如下:
登录alibaba在github中的wiki网站,可以看到版本对照表,如下图所示,以Spring Cloud Alibaba Version为中介,Spring Cloud Version、Spring Boot Version和Nacos各版本可以通过Spring Cloud Alibaba Version关联起来。
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
图1
图2
我们通过IDEA进行实验,实验步骤如下:
Step 1:创建Maven工程
创建父工程SCND和子工程SCNR,整体项目结构如下:
图3 项目结构
Step 2:添加父工程POM
添加父工程POM内容,可以看到Nacos与Spring初步集成只需要引入3个:
<?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>
<groupId>SCN</groupId>
<artifactId>SCND</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<nacos.version>0.2.2.RELEASE</nacos.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Step 3:添加子工程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">
<parent>
<artifactId>SCND</artifactId>
<groupId>SCN</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SCNR</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
图4 子工程结构
图5 Maven依赖
Step 4:创建启动文件
在子工程中创建java文件,填写如下代码:
package com.SCND;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Rigestry {
public static void main(String[] args) {
SpringApplication.run(Rigestry.class, args);
}
}
Step 5:创建application.yml
图6 application.yml
Step 6:验证结果
启动项目后,登录Nacos管理界面可以看到服务注册成功
图7 Nacos管理界面
这应该是最简单的Nacos集成SpringCloud了,后续会继续更新