Spring Cloud进阶之路 | 二:服务提供者(discovery)

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();
    }

}
扫描二维码关注公众号,回复: 9084856 查看本文章

启动类

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

源码

欢迎点赞

源码

github

https://gitee.com/xbd521/SpringCloudLearning

gitee

 https://github.com/liuminglei/SpringCloudLearning

本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。

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

image.png

 

发布了29 篇原创文章 · 获赞 1 · 访问量 2265

猜你喜欢

转载自blog.csdn.net/liuminglei1987/article/details/103632478