zoukankan      html  css  js  c++  java
  • Spring Cloud进阶之路 | 二:服务提供者(discovery)

    转载请注明作者及出处:

    作者:银河架构师

    原文链接:https://www.cnblogs.com/luas/p/12073156.html

    1 创建父项目

    以前文所述,以spring boot 2.1.7.RELEASE为基,spring cloud版本为Greenwich.SR2,spring cloud alibaba版本为2.1.0.RELEASE。后续均以此为版本参照,不再赘述。

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <groupId>com.luas.cloud</groupId>
        <artifactId>java-boot-parent-2.1</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>java-boot-parent-2.1</name>
        <description>Spring Cloud Learning, parent project by spring boot 2.1.x</description>
    
        <properties>
            <java.version>1.8</java.version>
            <hutool.version>4.6.13</hutool.version>
            <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
            <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>cn.hutool</groupId>
                    <artifactId>hutool-all</artifactId>
                    <version>${hutool.version}</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>
            </dependencies>
        </dependencyManagement>
    
    </project>
     

    2 创建商品工程

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.luas.cloud</groupId>
            <artifactId>java-boot-parent-2.1</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../../java-boot-parent-2.1</relativePath>
        </parent>
    
        <groupId>com.luas.cloud</groupId>
        <artifactId>xmall-product</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    
        <name>xmall-product</name>
        <description>Spring Cloud Learning,nacos-client</description>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- nacos cloud -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    注意,父工程便是上一步创建的父项目,特别注意一下relativePath,不如特殊指定,需要将父工程deploy到maven私服,子工程才能正常引用。

    application配置

    server:
      port: 8080
    
    spring:
      application:
        name: xmall-product
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848

    注意,spring.application.name将作为xmall-product服务在nacos注册后的服务名称,该服务名称非常重要,后续无论是通过feign调用,还是rest调用,均需要此名称。

    商品服务

    package com.luas.cloud.controller;
    
    import cn.hutool.core.map.MapUtil;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/sku")
    public class SkuController {
    
        @RequestMapping("/{skuId}")
        public Object get(@PathVariable("skuId") String skuId) {
            return MapUtil.<String, Object>builder()
                    .put("skuId", skuId)
                    .put("name", "笔记本电脑")
                    .put("price", 1199.00)
                    .build();
        }
    
    }

    启动类

    package com.luas.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class XmallProductApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(XmallProductApplication.class, args);
        }
    
    }

    关于@EnableDiscoveryClient注解,此处并没有添加,而服务依然能正常注册到nacos。什么原因呢?看如下两段代码即可明了:

    image.png

    image.png

    image.png

    此3处重要逻辑,均不依赖于是否配置了开启参数,所以会自动注册并监听。

    Nacos服务列表

    打开nacos控制台,发现访问已然在列表中,注册成功!

    image.png

    可以点击详情查看服务元数据

    访问

    访问http://localhost:8080/sku/1000000,得出现如下结果:

    image.png

    源码

    欢迎点赞

    1. github:https://gitee.com/xbd521/SpringCloudLearning
    2. gitee: https://github.com/liuminglei/SpringCloudLearning

    微信搜索【银河架构师】,发现更多精彩内容。

     技术资料领取方法:关注公众号,回复微服务,领取微服务相关电子书;回复MK精讲,领取MK精讲系列电子书;回复JAVA 进阶,领取JAVA进阶知识相关电子书;回复JAVA面试,领取JAVA面试相关电子书,回复JAVA WEB领取JAVA WEB相关电子书。

    image.png

  • 相关阅读:
    SQL Server 中的事务与事务隔离级别以及如何理解脏读, 未提交读,不可重复读和幻读产生的过程和原因
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSIS 系列
    微软BI 之SSAS 系列
    微软BI 之SSRS 系列
    微软BI 之SSRS 系列
    配置 SQL Server Email 发送以及 Job 的 Notification通知功能
  • 原文地址:https://www.cnblogs.com/luas/p/12073156.html
Copyright © 2011-2022 走看看