SpringCloud初体验:四、API GateWay 服务网关
网关服务很多,比如:Zuul、Kong、spring cloud gateway ……, 这里不纠结哪种性能好,本次体验是用的 spring cloud gateway
更多网关比较可以了解这篇文章: 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异
配置网关服务步骤:
1、初始化一个 SpringBoot 应用
2、引入相关依赖
<?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.zjj7</groupId>
<artifactId>gateway_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>gateway_server</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>
</properties> <!--配置仓库-->
<repositories>
<repository>
<id>aliRepository</id>
<name>aliRepository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <!--cloud 引来-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>--> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
pom.xml 的配置代码

3、修改配置文件
spring:
application:
name: gateway_server
cloud:
gateway:
default-filters:
routes:
- id: user-server
# lb代表从注册中心获取服务
uri: lb://user
predicates:
# 转发该路径
- Path=/user/**
# 带前缀
filters:
- StripPrefix=1
- id: publish-server
uri: lb://publish
predicates:
- Path=/publish/**
filters:
- StripPrefix=1 #服务注册中心端口号
server:
port: 6400 #服务注册中心实例的主机名、端口
#是否向服务注册中心注册自己
#是否检索服务
#服务注册中心的配置内容,指定服务注册中心的位置
eureka:
port:
6110
instance:
hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/
application.yml 的 相关配置

这里和其它服务不同的是: 启动类上面除了默认注解,无需添加更多
这时,一个简单的网关服务就搭建完了, 启动后所有的 API 就能以网关服务的地址为入口开始访问了
SpringCloud初体验:四、API GateWay 服务网关的更多相关文章
- 再谈API GateWay服务网关
前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...
- SpringCloud初体验:五、Sidecar 将 PHP 这类非 Java 生态语言的服务接入 Spring Cloud
先起一个 Sidecar 服务,一个PHP服务一个应用,和PHP服务部署在同一台机子,通过 localhost 访问,这样就解决了网络开销,相当于本地进程间调用 Sidecar 服务比较简单, 1.这 ...
- Spring Cloud 系列之 Gateway 服务网关(四)
本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) Spring Cl ...
- Spring Cloud Gateway 服务网关快速上手
Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...
- Spring Cloud 系列之 Gateway 服务网关(三)
本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) 本篇文章讲解 Ga ...
- Spring Cloud 系列之 Gateway 服务网关(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一) 本篇文章讲解 Gateway 网关的多种路由规则.动态路由规则(配合服务发现的路由规则 ...
- SpringCloud初体验:前言
体验了一天 SpringCloud 后发现,人们所讲的微服务架构不是一门技术,而是一种风格. 感觉确实可以这么认同,因为一套 SpringCloud 玩下来(未深入.未完整「链路追踪.动态刷新配置…… ...
- Spring Cloud 系列之 Gateway 服务网关(一)
什么是 Spring Cloud Gateway Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由 ...
- spring cloud微服务快速教程之(十) gateway 服务网关
0.前言 gateway是spring的二代网关, 作为Netflix Zuul的替代者,是异步非阻塞网关 ,ZUUL2也是异步非阻塞的,但未纳入spring cloud整合计划 基于WebFlux ...
随机推荐
- linux及安全《Linux内核设计与实现》第二章——20135227黄晓妍
第二章:从内核出发 2.1获取源代码 2.1.1使用git Git:内核开发者们用来管理Linux内核源代码的控制系统. 我们使用git来下载和管理Linux源代码. 2.1.2安装内核源代码(如果使 ...
- kali_常用软件记录
可参考:http://www.lvzejun.cn/2015/03/31/ubuntu-software/ 录屏软件 http://www.kohaupt-online.de/hp/ http://l ...
- POJ 1730 Perfect Pth Powers(唯一分解定理)
http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数, ...
- ${user.home} is not working in jenkins windows system
default setting create m2 in C:\Windows\system32\config\systemprofile change it to <localReposito ...
- 导出csv文件,导出axlsx文件。gem 'Axlsx-Rails' (470🌟);导入csv文件。
汇出 CSV 档案 需求:后台可以汇出报名资料 有时候后台功能做再多,也不如 Microsoft Excel 或 Apple Numbers 试算表软件提供的分析功能,这时候如果有汇出功能,就可以很方 ...
- c#LINQ表达树
如果你已经用过LINQ, 你应该知道函数方式,以及包含的丰富的类库, 如果你仍不了解, 那根据下面的链接去熟悉一下 the LINQ tutorial, lambda. 表达树提供了丰富的包含参数的 ...
- VirtualBox使用物理硬盘建立磁盘
VirtualBox,只能用命令行来 建立磁盘才可以使用物理硬盘. 1.运行cmd,cd进入你的VirtualBox目录,如: cd C:\Program Files\Sun\VirtualBox ...
- 046——VUE中组件之使用动态组件灵活设置页面布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 041——VUE中组件之pros数据的多种验证机制实例详解
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- EPANET头文件解读系列3——TOOLKIT.H
/******************************************************************** TOOLKIT.H - Prototypes for EPA ...