美文网首页
Spring Cloud Nacos注册中心搭建

Spring Cloud Nacos注册中心搭建

作者: _哎呀小心杆 | 来源:发表于2020-09-03 12:02 被阅读0次

一. 创建父工程

选择File->Project->maven 创建maven父工程,在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>

    <groupId>org.aiya</groupId>
    <artifactId>ai-ya-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>aiya</name>
    <description>parent for all project</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath></relativePath> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    <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>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1.relativePath 标签作用是依赖选择先从relativePath标签中地址选择->本地仓库->远程仓库,当标签内容为空时,从远程仓库下载。

2.dependencyManagement 和dependencies标签的区别
(1)dependencies:自动引入声明在dependencies里的所有依赖,并默认被所有的子项目继承。如果项目中不写依赖项,则会从父项目继承(属性全部继承)声明在父项目dependencies里的依赖项。
(2)dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要的依赖。如果不在子项目中声明依赖,是不会从父项目中继承的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。同时dependencyManagement让子项目引用依赖,而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个
dependencyManagement元素中指定的版本号,实现所有子项目使用的依赖项为同一版本。
(3)dependencyManagement 中的 dependencies 并不影响项目的依赖项;而独立dependencies元素则影响项目的依赖项。只有当外层的dependencies元素中没有指明版本信息时,dependencyManagement 中的 dependencies 元素才起作用。一个是项目依赖,一个是maven项目多模块情况时作依赖管理控制的。

二. 创建模块Nacos注册中心。

右键父工程 New->modules->Spring initializr(或者maven都可以)
在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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.aiya</groupId>
        <artifactId>ai-ya-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.aiya</groupId>
    <artifactId>ai-ya-registry-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>registry</name>
    <description>Registry Server project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!--引入nacos依赖-->
    <dependencies>
                <!--注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!--配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

增加配置文件bootstrap.yml 因为bootstrap.yml优先于application.yml加载 然后在bootstrap.yml里添加配置

spring:
  application:
    name: aiya-client  # 注册的服务名称
  cloud:
    nacos:
   #这是注册中心配置
      discovery:
        server-addr: localhost:8848   # nacos服务中心地址
   #这是配置中心配置
      config:
        server-addr: localhost:8848
        file-extension: yaml

配置中心配置文件目前支持yaml,porperites两种格式 配置文件的名字为${spring.application.name}-${spring.profiles.active}-${file-extension},其中${spring.profiles.active}可以省略 即该注册中心的配置应该是 aiya-client.yaml
然后在启动类加上注解@EnableDiscoveryClient 就可以把当前服务注册到nacos了

相关文章

网友评论

      本文标题:Spring Cloud Nacos注册中心搭建

      本文链接:https://www.haomeiwen.com/subject/rvgfjktx.html