Spring Cloud Advanced Road | two: Service Provider (discovery)

1 Create the parent project

 

Described previously, in order for the spring boot 2.1.7.RELEASE group, spring cloud version Greenwich.SR2, spring cloud alibaba version 2.1.0.RELEASE. Follow-up on this version are referring to, not repeat them.

 

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 Create Product Engineering

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>

 

Note that the parent project is one step on the parent project created special attention to relativePath, better to specify special, you need to deploy to the parent project maven PW, sub-projects in order to properly cited.

 

application configuration

server:
  port: 8080

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

Note, spring.application.name as xmall-product service service name after nacos registered, the service name is very important, whether through feign follow-up call or rest is called, requires this name.

 

Goods Services

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

}

 

Startup class

 

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

}

 

About @EnableDiscoveryClient comment here does not add, but normal service was still able to register to nacos. What causes it? See the following two pieces of code can be understood:

 

image.png

 

image.png

 

image.png

 

3 important at this logic, are not dependent on whether the open configuration parameters, it will be automatically registered and monitored.

 

Nacos list of services

 

Open nacos console, access is already found in the list, the registration is successful!

 

image.png

 

 

Details can click view service metadata

 

 

access

 

Visit http: // localhost: 8080 / sku / 1000000, you have to appear the following results:

 

image.png

 

 

Source

 

Welcome thumbs up

Source

github

https://gitee.com/xbd521/SpringCloudLearning

gitee

 https://github.com/liuminglei/SpringCloudLearning

 

This article is [Galaxy] original architect, in an article for reprint please indicate the author and source of the obvious.

Micro-channel search [architect] Galaxy, find more exciting content.

image.png

 

Published 29 original articles · won praise 1 · views 2265

Guess you like

Origin blog.csdn.net/liuminglei1987/article/details/103632478