前言
SpringCloud 是微服务中的翘楚,最佳的落地方案。
Spring Cloud Gateway 是 Spring Cloud 新推出的网关框架,之前是 Netflix Zuul。网关通常在项目中为了简化
前端的调用逻辑,同时也简化内部服务之间互相调用的复杂度;具体作用就是转发服务,接收并转发所有内外
部的客户端调用;其他常见的功能还有权限认证,限流控制等等。
本博客会提到网关的基本转发功能,熔断功能,限流功能以及功能的综合使用。
后期文章会首发于本专栏,也会不定时发放福利 ,欢迎关注
JAVA高级进阶zhuanlan.zhihu.com环境
JDK 1.8.0 +
Maven 3.0 +
SpringBoot 2.0.3
SpringCloud Finchley.RELEASE
Redis 3.0 +
开发工具
IntelliJ IDEA
正文
commons 工程
commons 工程 - 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-commons</artifactId>
<version>1.0</version>
<!-- 工程名称和描述 -->
<name>springcloud-gateway-commons</name>
<description>公用工程</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties 下声明相应的版本信息,然后在 dependency 下引用的时候用 ${} 就可以引入该版本 jar 包了 -->
<properties>
<!-- 编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk -->
<java.version>1.8</java.version>
<!-- SpringBoot -->
<platform-bom.version>Cairo-SR3</platform-bom.version>
<!-- SpringCloud -->
<spring-cloud-dependencies.version>Finchley.RELEASE</spring-cloud-dependencies.version>
</properties>
<!-- 加入依赖 -->
<dependencies>
</dependencies>
<!-- 依赖 jar 包版本管理的管理器 -->
<!-- 如果 dependencies 里的 dependency 自己没有声明 version 元素,那么 maven 就此处来找版本声明。 -->
<!-- 如果有,就会继承它;如果没有就会报错,告诉你没有版本信息 -->
<!-- 优先级:如果 dependencies 里的 dependency 已经声明了版本信息,就不会生效此处的版本信息了 -->
<dependencyManagement>
<dependencies>
<!-- SpringBoot -->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${
platform-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${
spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件依赖 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置一些共用依赖
service 工程
① 此工程下有四个模块:一个注册中心,一个网关以及两个提供者
② 两个提供者除端口不一致以外,其他代码基本一致
registry-service(注册中心)
registry-service - 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 继承父 -->
<parent>
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-service</artifactId>
<version>1.0</version>
</parent>
<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-registry-service</artifactId>
<version>1.0</version>
<!-- 工程名称描述 -->
<name>springcloud-gateway-registry-service</name>
<description>注册中心</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
<properties>
</properties>
<!-- 加入依赖 -->
<dependencies>
<!-- 服务注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 插件依赖 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要主要加入 spring-cloud-starter-netflix-eureka-server 依赖
registry-service - application.yml 配置文件主要加入 spring-cloud-starter-netflix-eureka-server 依赖
- 主要加入 spring-cloud-starter-netflix-eureka-server 依赖
registry-service - application.yml 配置文件
# 端口
server:
port: 8761
# 应用名称
spring:
application:
name: eureka-server
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
# 是否向注册中心注册自己
registerWithEureka: false
# 是否向注册中心获取注册信息
fetchRegistry: false
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致
registry-service - 启动类
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudGatewayRegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudGatewayRegistryServiceApplication.class, args);
}
}
- 在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心
registry-service - 启动项目
1. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面
注:由于服务工程 A 和服务工程 B 除端口不一致以外,其他代码基本一致,所以服务工程 B 不再赘述
a-service(服务工程 A)
a-service - 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 继承父 -->
<parent>
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-a-service</artifactId>
<version>1.0</version>
</parent>
<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-a-service-core</artifactId>
<version>1.0</version>
<!-- 工程名称描述 -->
<name>springcloud-gateway-a-service-core</name>
<description>服务工程 - A 核心</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下声明相应的版本信息,然后在dependency下引用的时候用 ${} 就可以引入该版本jar包了 -->
<properties>
</properties>
<!-- 加入依赖 -->
<dependencies>
<!-- commons工程 依赖 -->
<dependency>
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-commons</artifactId>
<version>1.0</version>
</dependency>
<!-- api工程 依赖 -->
<dependency>
<groupId>com.zwc</groupId>
<artifactId>springcloud-gateway-a-service-api</artifactId>
<version>1.0</version>
</dependency>
<!-- springboot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 提供者消费者 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependenci