spring cloud微服务分布式云架构-Gateway入门
Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。
入门案例
接下来,介绍一下简单使用SpringCloud Gateway路由功能
创建项目
新建一个项目,项目种加入SpringCloud Gateway依赖,完整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.dalaoyang</groupId>
<artifactId>springcloud_gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_gateway</name>
<description>springcloud_gateway</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
SpringBoot主程序
在类中配置路由
主程序中加入了一种配置路由的方法,利用@Bean的方式自定义RouteLocator。
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringcloudGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudGatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/jianshu")
.uri("http://www.jianshu.com/u/128b6effde53")
).build();
}
}
在配置文件中配置:
server:
port: 8888
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: dalaoyang
uri: http://www.dalaoyang.cn/
predicates:
- Path=/dalaoyang/**
- id: juejin
uri: https://juejin.im/user/5aa50b96f265da23866f836e
predicates:
- Path=/juejin/**
//Spring Cloud大型企业分布式微服务云架构源码
//请加一七九一七四三三八零
到这里配置完成了。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
spring cloud微服务分布式云架构--hystrix的使用
hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足) 接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能: 服务消费者根据Id调用服务提供者的接口,获取User表单的对应的记录,若请求超时则返回id为-1的User记录 一、基于Ribbon <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEnco...
-
下一篇
3月20日云栖精选夜读 | 阿里开源自用 OpenJDK 版本,Java 社区迎来中国力量
【点击订阅云栖夜读周刊】 阿里开源自用 OpenJDK 版本,Java 社区迎来中国力量 3 月 21 日,阿里巴巴将宣布开源 Alibaba Dragonwell。届时,开发者可通过阿里云开发者中心及 Github 社区下载使用。 热点热议 阿里开源自用 OpenJDK 版本,Java 社区迎来中国力量 作者:amber涂南发表在:阿里系统软件技术 阿里巴巴联合汉仪重磅推出五款人工智能字体:汉仪天真体、英雄体等 作者:技术小能手 专访百胜软件CTO:30万家门店,从MySQL到阿里云POLARDB云原生数据库 作者:技术小能手 知识整理 如何在E-MapReduce中进行Kafka集群间数据复制 作者:鱼跟猫发表在:阿里云E-MapReduce PHP使用Beanstalkd实例 作者:再现理想 CentOS编译和yum安装Beanstalkd及service和systemctl管理 作者:再现理想 JavaScript 教程 作者:webmirror CentOS6.9搭建Git服务器并使用Gitolite配置权限 作者:再现理想 美文回顾 5G万物互联时代,物联网设备需警惕DDo...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- 2048小游戏-低调大师作品
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合Redis,开启缓存,提高访问速度
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2更换Tomcat为Jetty,小型站点的福音