美文网首页拉勾教育学习笔记
Spring Cloud Alibaba - Nacos服务提供

Spring Cloud Alibaba - Nacos服务提供

作者: 耳威巴帝 | 来源:发表于2020-11-14 21:23 被阅读0次

    一、Nacos服务提供者

    Nacos兼具注册中心

    二、前期代码准备

    后续Spring Cloud Alibaba开发都是基于本套代码上开发

    2.1, 构架Maven项目,取名sca-partner

    • 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>com.ydw</groupId>
        <artifactId>sca-partner</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 子工程 -->
        <modules>
            <module>sca-service-8081</module>
        </modules>
    
        <!--父工程打包方式为pom-->
        <packaging>pom</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.5.RELEASE</version>
        </parent>
    
        <properties>
            <java.version>11</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <!--SCA -->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.1.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
            <!--SCA -->
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!--web依赖-->
            <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>
    
        <build>
            <plugins>
                <!--编译插件-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                        <encoding>utf-8</encoding>
                    </configuration>
                </plugin>
                <!--打包插件-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <!-- 配置是否被传播到子POM,后续dubbo的api服务会因为pom打包方式导致无法install -->
                    <inherited>false</inherited>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    三、Nacos服务提供者开发

    3.1,构架SpringBoot项目,取名sca-service-8081(取名建议带上端口号,后续微服务太多方便查询)

    • 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">
    
        <parent>
            <artifactId>sca-partner</artifactId>
            <groupId>com.ydw</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>sca-service-8081</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
        </dependencies>
    </project>
    
    • application.yml
    server:
      port: 8081  # 端口
    spring:
      application:
        name: sca-service # 服务名,同是也会作为Nacos注册中心的服务名
      # nacos配置
      cloud:
        nacos:
          discovery:
            # 集群中各节点信息都配置在这里(集群用,隔开)
            server-addr: 127.0.0.1:8848
    
    • ScaService8081Application.java启动类
    package com.ydw.sca;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient //通用注册中心注解
    public class ScaService8081Application {
        public static void main(String[] args) {
            SpringApplication.run(ScaService8081Application.class, args);
        }
    }
    
    • 启动成功后,会在注册中心上面看到服务名


      image.png

    相关文章

      网友评论

        本文标题:Spring Cloud Alibaba - Nacos服务提供

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