Spring Cloud服务之Nacos作为注册中心与配置中心

1、创建maven父工程管理jar包版本

创建maven骨架,删除多余部分文件。只留pom文件,添加依赖

  <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>


    <!--版本管理-->
    <properties>
        <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR9</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
    </properties>

    <!--jar包管理-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.itmy</groupId>
                <artifactId>common-module</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

            <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>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.16</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.26</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.3</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.83</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.16</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

2、创建子工程模块

  <modules>
        <module>user-module</module>
        <module>order-module</module>
        <module>common-module</module>
        <module>gateway-module</module>
    </modules>

3、配置参数

创建好模块之后,下面就需要配置连接参数,这里依user-module子模块为例

添加maven依赖

 <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.itmy</groupId>
            <artifactId>common-module</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--Nacos Config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--SpringBoot2.4.x的版本之后 使用bootstrap需要加包-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!--            <version>3.0.1</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

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

创建资源文件bootstrap.yml文件

启动类加@EnableDiscoveryClient 注解,注册到注册中心

配置bootstrap.yml文件

server:
  port: 8801

spring:
  profiles:
    active: dev

  application:
    # 服务名称
    name: user-service-model

配置bootstrap-dev.yml文件

spring:
  cloud:
    nacos:
      # 服务注册
      discovery:
        server-addr: 127.0.0.1:8848
        # 分组
        group: dev
        # 注册到nacos上的服务名称,默认{${spring.application.name}
        service: ${spring.application.name}
        namespace: 23857f22-27ac-4947-988a-1b88d4eeb807
      # 读取nacos上的服务配置
      config:
        server-addr: 127.0.0.1:8848
        # 分组
        group: dev
        file-extension: yaml
        namespace: 23857f22-27ac-4947-988a-1b88d4eeb807
        # 共享配置
        shared-configs:
          - data-id: datasource.yaml
            group: dev
            refresh: true
          - data-id: mybatis.yaml
            group: dev
            refresh: true
        # 扩展配置
        extension-configs:
          - data-id: user-config-dev.yaml
            group: dev
            refresh: true

4、启动服务

5、登录Nacos查看注册中心

 

如上图,说明服务注册成功。

6、查看配置中心是否启用成功

登录Nacos创建配置文件

创建成功后,启动服务使用postman调用接口

数据获取成功

 

posted @ 2023-10-11 15:28  sowler  阅读(227)  评论(0编辑  收藏  举报