[The Road to Java Spring Cloud] - Creation using Nacos and Gateway Center

0. Preface

In the previous section, we created a project architecture in which subsequent projects will be supplemented.

1. Nacos

1.1 Introduction

Nacos can be used to discover, configure, and manage microservices.Provides a set of easy-to-use feature sets that enable fast implementation of dynamic service discovery, service configuration, service metadata, and traffic management.

Nacos is used to build, deliver, and manage microservice platforms more quickly and easily.Nacos is the service infrastructure for building a "service-centric" modern application architecture, such as the micro-service paradigm and the cloud native paradigm.

This is what we usually call a Configuration Center and a Service Discovery Center.

1.2 Build and start up

The current version of Nacos does not support creating a service as a Spring boot. It must be run as a Java package alone or as a Docker service. Let's give you an overview of running locally.

Download the installation package:

curl https://github.com/alibaba/nacos/releases/download/1.2.1/nacos-server-1.2.1.zip
unzip nacos-server-$version.zip perhaps tar -xvf nacos-server-$version.tar.gz
cd nacos/bin

Install using source code:

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  
ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin

Start:

Linux/Unix/Mac

Start command (standalone stands for single machine mode, non-cluster mode):

sh startup.sh -m standalone

If you are using a ubuntu system or running a script with an error message [[Symbol not found, try running as follows:

bash startup.sh -m standalone

Windows

Start command:

cmd startup.cmd

Or double-clickStartup.cmdRun the file.

2. Spring Cloud Gateway

The entire gateway service we use is the Spring Cloud Gateway.In Spring Cloud Micro Services, the entire system only exposes gateways to the outside world, while other services are not visible to the outside world.So we need to set up a gateway service that we can use.

Create a gateway directory under nature/manager and addPom.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>
        <groupId>club.attachie</groupId>
        <artifactId>manager</artifactId>
        <version>${revision}</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>club.attachie</groupId>
    <artifactId>gateway</artifactId>
    <packaging>jar</packaging>
    <version>${revision}</version>

</project>

Register the module under manager:

<modules>
    <module>gateway</module>
</modules>

2.1 Add Gateway

After the project is created, you need to add dependent packages:

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

In the gateway project, create the following directory:

├── pom.xml
└── src
    └── main
        ├── java
        │   └── club
        │       └── attachie
        │           └── gateway
        │               └── SpringGatewayApplication.java
        └── resources
            └── bootstrap.yml

Establish SpringGateAppliction.java File, code as follows:

package club.attachie.gateway;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;

/**
 * @author attaching
 */
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class SpringGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringGatewayApplication.class, args);
    }
}

Create in the resource directoryBootstrap.yml:

spring:
  application:
    name: gateway

yml is a Spring configuration file format with the names application and bootstrap, which are loaded first than application.

2.2 Add nacos

First in nature/Pom.xmlAdd nacos version number:

<nacos.version>2.2.1.RELEASE</nacos.version>

Then add nacos-related dependency management under dependency Management > dependencies:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>${nacos.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-starters</artifactId>
    <version>${nacos.version}</version>
</dependency>

In the Gateway projectPom.xmlAdd to:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Then come back and set it in bootstrap:

spring:
  application:
    name: gateway

  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848

3 Summary

This concludes with the introduction of nacos configuration and Gateway applications, as individuals have not done much in-depth research on related technologies, so they can only do so now.Subsequent research is in-depth and will be supplemented in this series.

Please pay attention to more content My blog, Mr. Gao's Cabin

Tags: Java Spring Maven xml

Posted on Wed, 03 Jun 2020 22:42:50 -0400 by petersro