【Java Spring Cloud 实战之路】- 使用Nacos和网关中心的建立

0. 前言

在上一节中,咱们建立了一个项目架构,后续的项目都会在那个架构上作补充。java

1. Nacos

1.1 简介

Nacos能够用来发现、配置和管理微服务。提供了一组简单易用的特性集,能够快速实现动态服务发现、服务配置、服务元数据及流量管理。git

Nacos用来更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以”服务“为中心的现代应用构架(例如微服务范式、云原生范式)的服务基础设置。github

也就是一般咱们所说的配置中心和服务发现中心。spring

图片

1.2 搭建和启动

Nacos目前版本不支持以Spring boot的形式建立服务,必须以一个Java包的形式单独运行或者以Docker服务的形式运行,咱们大概讲解一下本地运行。apache

下载安装包:bootstrap

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

使用源码安装:ubuntu

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

启动:bash

Linux/Unix/Mac架构

启动命令(standalone表明着单机模式运行,非集群模式):app

sh startup.sh -m standalone

若是您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试以下运行:

bash startup.sh -m standalone

Windows

启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

2. Spring Cloud Gateway

整个的网关服务,咱们采用的Spring Cloud Gateway。在Spring Cloud微服务里,整个系统只对外公开了网关,其余的服务是对外不可见的。因此须要设置一个让咱们能够用的网关服务。

在 nature/manager下建立一个gateway目录,并添加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>

在manager下注册该模块:

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

2.1 添加 Gateway

建立完成项目后,须要添加依赖包:

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

在gateway项目中,建立以下目录:

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

建立 SpringGateAppliction.java文件,代码以下:

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

在resource目录下建立 bootstrap.yml:

spring:
 application:
   name: gateway

yml 是Spring 的一种配置文件格式,其中名称有application和bootstrap,bootstrap比application先加载。

2.2 添加 nacos

先在 nature/pom.xml 添加 nacos 版本号:

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

而后在dependencyManagement > dependencies 下添加 nacos相关依赖管理:

<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>

在Gateway项目中pom.xml 添加:

<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>

而后回过头来,在bootstrap里设置:

spring:
 application:
   name: gateway

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

3 总结

nacos的配置和Gateway应用的介绍就到这里为止了,由于我的并未对相关技术进行过多深刻的研究,因此目前只能作到这些。后续研究深刻了,会在这个系列中补齐的。

相关文章
相关标签/搜索