使用nacos进行服务注册的配置

本文仅仅实现服务注册功能

自己在实现注册功能的时候,因为springInitializr失效,于是我就只能创建maven来改造成spring

在这过程中,我一直认为的是只需要加spring-boot-starter-boot的依赖就可以了,导致项目一直报错http包缺少依赖,虽然我手动添加了http但是总感觉哪里不对劲

经过搜索之后,发现是需要引入一个spring-boot-starter-parent的依赖才算是真正改造完成

然后nacos的官方文档和之前有一些出入,但是博客上很多配置文件都一致的省略了依赖的version值,而且对于pom.xml文件没有任何解释,不明就里,也让实现的功能有点麻烦

因此经过多番尝试,我找到了能够实现nacos服务注册的最简配置

-----------------------------实现过程--------------------------

1.首先创建一个springboot工程(如果https://start.spring.io失效导致无法初始化,那么就创建maven工程)

2.无论如何,项目的pom.xml中需要含有下面三个最简依赖。

  首先是spring-boot-starter-parent

  然后是spring-boot-starter-web

  最后是nacos的依赖

<?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>com.test</groupId>
    <artifactId>nacostest_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
     </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
    </dependencies>
</project>

3.之后启动nacos的客户端

4.在spirngboot工程的启动类上加上@EnableDiscoveryClient注解,启动项目即可在nacos网页看到注册已经成功

 

原文地址:https://www.cnblogs.com/skyvalley/p/14195350.html