0. Preface
In the previous section, we created a project architecture, which will be supplemented by subsequent projects.
1. Nacos
1.1 INTRODUCTION
Nacos can be used to discover, configure, and manage microservices. It provides a set of simple and easy-to-use feature sets, which can quickly realize 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" centered modern application architecture (such as micro service paradigm and cloud native paradigm).
That is to say, the configuration center and service discovery center.
Note: light theory is not enough. In this free gift of 5 JAVA architecture project practical course and large factory interview question bank, interested can get into skirt 783802103, do not enter without foundation!
1.2 build and start up
The current version of Nacos does not support the creation of services in the form of Spring boot. It must be run in the form of a Java package alone or in the form of a Docker service. Let's talk about local running.
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 stand-alone mode operation, non cluster mode):
sh startup.sh -m standalone
If you are using the ubuntu system, or running the script with an error message [[the symbol cannot be found, try running as follows:
bash startup.sh -m standalone
Windows
Start command:
cmd startup.cmd
Or double click startup.cmd Run the file.
2. Spring Cloud Gateway
For the whole gateway service, we adopt Spring Cloud Gateway. In Spring Cloud microservices, the whole system only exposes the gateway to the outside world, and other services are invisible 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 add pom.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 creating the project, you need to add a dependency package:
<!-- 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 resource directory bootstrap.yml :
spring: application: name: gateway
yml is a configuration file format of Spring. Its name includes application and bootstrap. Bootstrap loads before application.
2.2 add nacos
First in nature/pom.xml Add 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 project pom.xml add 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 turn around and set in bootstrap:
spring: application: name: gateway cloud: nacos: config: server-addr: 127.0.0.1:8848
3 Summary
So far, the configuration of nacos and the introduction of Gateway application have been introduced. Because individuals have not conducted too much in-depth research on related technologies, so they can only do so at present. The follow-up research is in-depth and will be supplemented in this series.
Finally note: the theory of light is not enough. In this free gift of 5 JAVA architecture project practical course and large factory interview question bank, interested can get into skirt 783802103, do not enter without foundation!
The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling