Spring Cloud Alibaba nacos configuration center

background

In the last article, we talked about how to build a registry. This time, we talked about how to use nacos as a registry.

Spring cloud Alibaba basis to create basic dependency

First of all, we create a spring cloud Alibaba basis basic dependency project to work out the common version we want to use.

  • spring boot version 2.1.7.RELEASE
  • spring cloud version Greenwich.RELEASE
  • spring cloud Alibaba version 2.1.0.RELEASE
  • Spring IO Platform version depends on
   <modelVersion>4.0.0</modelVersion>

    <groupId>com.xian.cloud</groupId>
    <artifactId>spring-cloud-alibaba-basis</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>spring cloud alibaba total pom</name>
    <description>spring cloud alibaba Tutorial total pom version control</description>
    <modules>
        <module>cloud-discovery-server</module>
        <module>cloud-discovery-client-common</module>
    </modules>
    <!--<parent>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-parent</artifactId>-->
        <!--<version>2.1.7.RELEASE</version>-->
        <!--<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    <!--</parent>-->

    <properties>
        <!-- Basic attributes -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <!-- Version attribute -->
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <spring-boot.version>2.1.7.RELEASE</spring-boot.version>
        <spring-platform.version>Cairo-SR8</spring-platform.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- spring-cloud-alibaba Total dependence -->
            <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>
            <!--Support Spring Boot 2.1.X-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--Greenwich.RELEASE-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--
            Spring IO Platform,It can be simply considered as a dependency maintenance platform, which gathers related dependencies together and provides a version number for each dependency.
            Complete dependency list https://docs.spring.io/platform/docs/current/reference/html/appendix-dependency-versions.html
             -->
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>${spring-platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
        <!-- Public dependent jar package -->
    <dependencies>
        <!-- nacos discovery -->
        <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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

Students who are interested in the jar package of Spring IO Platform can pay attention to the version management of the third-party common jar package. The third-party jar versions corresponding to each spring are listed above. This is convenient for us to manage the version of the third-party jar package. #To solve the version conflict of jar package
Besides the well-known ways, spin boot also has the spring boot dependencies dependency way.

So we're building two new module s.

Service provider cloud discovery server

  <parent>
        <groupId>com.xian.cloud</groupId>
        <artifactId>spring-cloud-alibaba-basis</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-discovery-server</artifactId>
    <description>Service provider</description>
    <name>Service provider</name>

Because we have defined the public dependent jar packages in the pom of the parent class, we don't need to introduce the jar packages again. We just need to formulate the parent pom to inherit these jar packages. In the same way, I will use maven's jar package delivery method to develop the case.

Create service startup class and controller class of external service

  • Startup class
package com.xian.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
 * @Author: xlr
 * @Date: Created in 2:44 PM 2019/10/27
 */
@EnableDiscoveryClient
@SpringBootApplication
public class DiscoveryServerApplication {


    public static void main(String[] args) {
        SpringApplication.run(DiscoveryServerApplication.class, args);
    }
}
  • http interface for external services
package com.xian.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xlr
 * @Date: Created in 2:57 PM 2019/10/27
 */
@RestController
@RequestMapping("server")
@Slf4j
public class DiscoverCotroller {


    /**
     * External service HTTP interface
     * @param name
     * @return
     */
    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        log.info("invoked name = " + name);
        return "hello " + name;
    }


}

Write YAML file

server:
  port: 9012

spring:
  profiles:
    active: dev
  application:
    name: cloud-discovery-server
  cloud:
    nacos:
      discovery:
        server-addr: 47.99.209.72:8848

Cloud discovery client for service consumers

    <parent>
        <artifactId>spring-cloud-alibaba-basis</artifactId>
        <groupId>com.xian.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloud-discovery-client</artifactId>
    <name>Service consumers</name>

Create the service startup class and call the controller http interface of the service provider

  • Startup class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Author: xlr
 * @Date: Created in 3:03 PM 2019/10/27
 */
@EnableDiscoveryClient
@SpringBootApplication
public class DiscoveryClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(DiscoveryClientApplication.class, args);
    }
}
  • Consumer service interface
package com.xian.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: xlr
 * @Date: Created in 3:04 PM 2019/10/27
 */
@RequestMapping("client")
@RestController
@Slf4j
public class DiscoveryClientController {

    //Service provider project name spring.application.name
    public static final String CLOUD_DISCOVERY_SERVER = "cloud-discovery-server";

    /**
     * A large number of abstract interfaces related to service governance are provided in Spring Cloud Commons, including DiscoveryClient, LoadBalancerClient, etc.
     * From the naming of LoadBalancerClient interface, it is an abstract definition of load balancing client.
     */
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String test() {
        ServiceInstance serviceInstance = loadBalancerClient.choose(CLOUD_DISCOVERY_SERVER);
        log.info( "ServiceInstance :{}",serviceInstance );
        String url = serviceInstance.getUri() + "/server/hello?name=" + "tom";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return "call " + url + ", Return : " + result;
    }
}

Write YAML file

server:
  port: 9011

spring:
  profiles:
    active: dev
  application:
    name: cloud-discovery-client
  cloud:
    nacos:
      discovery:
        server-addr: 47.99.209.72:8848

Then start the service
View nacos console

Shows that both services have been registered in the nacos registry

Then we ask http://localhost:9011/client/test to see if we can display the results we want.

By this time, we have completed our registration center. Whether the port can be load balanced can be changed at the service provider

The server has started 9013 and 9014 port services respectively.

Test again http://localhost:9011/client/test

By this time, my registry and load balancing have all been implemented.

Reference material

nacos official documents

Official document of spring cloud alibaba

Sample code

github

How do you like to share this public account?

Copyright notice: This is the original article of the blogger, following CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint. Please attach the QR code of the public account for reprint

Keywords: Java Spring Maven snapshot Lombok

Added by reaper7861 on Sun, 27 Oct 2019 18:42:05 +0200