网关服务很多,比如: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 服务网关的更多相关文章

  1. 再谈API GateWay服务网关

    前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...

  2. SpringCloud初体验:五、Sidecar 将 PHP 这类非 Java 生态语言的服务接入 Spring Cloud

    先起一个 Sidecar 服务,一个PHP服务一个应用,和PHP服务部署在同一台机子,通过 localhost 访问,这样就解决了网络开销,相当于本地进程间调用 Sidecar 服务比较简单, 1.这 ...

  3. Spring Cloud 系列之 Gateway 服务网关(四)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) Spring Cl ...

  4. Spring Cloud Gateway 服务网关快速上手

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...

  5. Spring Cloud 系列之 Gateway 服务网关(三)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) 本篇文章讲解 Ga ...

  6. Spring Cloud 系列之 Gateway 服务网关(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一) 本篇文章讲解 Gateway 网关的多种路由规则.动态路由规则(配合服务发现的路由规则 ...

  7. SpringCloud初体验:前言

    体验了一天 SpringCloud 后发现,人们所讲的微服务架构不是一门技术,而是一种风格. 感觉确实可以这么认同,因为一套 SpringCloud 玩下来(未深入.未完整「链路追踪.动态刷新配置…… ...

  8. Spring Cloud 系列之 Gateway 服务网关(一)

    什么是 Spring Cloud Gateway Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由 ...

  9. spring cloud微服务快速教程之(十) gateway 服务网关

    0.前言 gateway是spring的二代网关, 作为Netflix Zuul的替代者,是异步非阻塞网关 ,ZUUL2也是异步非阻塞的,但未纳入spring cloud整合计划 基于WebFlux  ...

随机推荐

  1. hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办

    1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ...

  2. 与R纠缠的两件事——rownames和子集--转载

    与R语言纠缠了一个星期,从快速上手的暗暗得意,到之后某些细节的纠结烦躁,过山车式体验中,我逐渐才认识了真实的R语言. 期间遇到两个很烦恼的问题,一个是让人烦躁抓狂,另一个是无意发现的重大错误. 1.  ...

  3. [java]No qualifying bean of type 解决方法

    1.错误原因:注解写错 2.原理如下: 现在的spring早就已经摆脱了之前一堆xml配置文件的情况,都是通过注解配置的方式进行依赖注入了,通常情况下,我们会有一个配置类,然后通过Annotation ...

  4. HIVE之常用字符串函数

    可以参考: 博文 : https://www.iteblog.com/archives/1639.html

  5. Kubernetes实践--hello world 示例

    本文所说的Hello world是一个web留言板应用,并且是基于PHP+Redis的两层分布式架构的web应用,前端PHP web网站通过访问后端Redis数据库完成用户留言的查询和添加功能,具备读 ...

  6. 007PHP文件处理—— 判断文件与操作文件fopen fread fclose is_executable is_readable is_writeable

    <?php /** * 判断文件与操作文件fopen fread fclose * is_executable is_readable is_writeable */ //判断一个文件是不是一个 ...

  7. 基于Open XML 导出数据到Excel

    数据导出的结果: 步骤1.新建一个Excel 文档,模板根据自己需要设置 步骤2.使用OpenXml  打开Excel 文件 步骤3.点击ReflectCode 功能,生成相应的代码文档 using ...

  8. 201621123010《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.面向接口编程 interface.implements has-a 关系 Comparable.Comparator 1. ...

  9. 【linux】linux无root权限安装包的一般流程

    apt-get source PACKAGE ./configure --prefix=$HOME/myapps make make install

  10. Win7系统Visual Studio 2013配置OpenCV3.1图文详解

    Win7系统Visual Studio 2013配置OpenCV3.1图文详解 OpenCV3.1对硬件加速和移动开发的支持相对于老版本都有了较大改进,支持新的开发工具,更易于扩展,配置方式也比以前简 ...