在微服务架构体系中,使用API 服务网关后的系统架构图如下:

API服务网关的主要作用如下:

  • 服务访问的统一入口
  • 服务访问的负载均衡功能
  • 服务访问的路由功能

在SpringCloud中,基于Netflix 和Zuul 组件来实现API 网关功能,下面先来构建一个服务网关项目:

1. pom文件


<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.3.RELEASE</version>
</parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

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

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependencies>

2. application.yml 配置

management:
endpoints:
web:
exposure:
include: "*"
spring:
application:
name: gateway server:
port: 8280 eureka:
client:
serviceUrl:
defaultZone: http://eurekaServer:8761/eureka/

3 . 网关服务启动类

@SpringBootApplication
@EnableZuulProxy
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args) ;
}
}

启动上述服务后,我们访问具体服务时只要按照如下的方式进行统一访问就可以了:

  http : // {api网关服务器域名} : {api网关服务监听端口号} / {服务在注册中心注册和服务名} / {服务的@RequestMapping配置的路径}

上面的形式说得专业些就是:http://[ zuul 路 由 服务器 地址]/[ serviceId]/[ 具体 服务 的 端点]

同时上面的这也就是网关提供的默认的路由访问规则 , 使用的是在 Eureka 中注册的微服务的ID, 有时候这种映射机制并不能满足需求,比如 对于 用户 服务, 我们 不想 使用 userservice 这个 路径, 而是

想更简单一点直接使用 user。 此时, 可以 在 Zuul 路由服务器 配置文件中通过增加格式 为“ zuul.routes.微服 务 d= 指定 路径” 的属性配置方式 进行配置,对访问路径进行控制。

配置完成后,我们可以通过  http://127.0.0.1:8280/actuator/routes 来查看网关当前的路由规则。

Spring Cloud 之 服务网关的更多相关文章

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

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

  2. Spring Cloud (14) 服务网关-过滤器

    Spring Cloud Zuul作为网关所具备的最基本的功能:路由,还具备另外一个核心的功能:过滤器. 过滤器 通过Spring Cloud Zuul实现的路由功能,我们的微服务可以通过统一的API ...

  3. Spring Cloud Gateway服务网关

    原文:https://www.cnblogs.com/ityouknow/p/10141740.html Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gatewa ...

  4. Spring Cloud (13) 服务网关-路由配置

    传统路由配置 所谓传统路由配置方式就是在不依赖于服务发现机制情况下,通过在配置文件中具体制定每个路由表达式与服务实例的映射关系来实现API网关对外部请求的路由.没有Eureka服务治理框架帮助的时候, ...

  5. Spring Cloud (12) 服务网关-基础

    通过前几篇介绍,已经可以构建一个简单的微服务架构了,如下图: 通过eureka实现服务注册中心以及服务注册发现,通过ribbon或feign实现服务的消费以及负载均衡,通过spring cloud c ...

  6. spring cloud:服务网关 Spring Cloud GateWay 入门

    Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...

  7. Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)

    导读 我们知道在基于Spring Cloud的微服务体系中,各个微服务除了在内部提供服务外,有些服务接口还需要直接提供给客户端,如Andirod.IOS.H5等等. 而一个很尴尬的境地是,如果直接将提 ...

  8. Spring Cloud 微服务三: API网关Spring cloud gateway

    前言:前面介绍了一款API网关组件zuul,不过发现spring cloud自己开发了一个新网关gateway,貌似要取代zuul,spring官网上也已经没有zuul的组件了(虽然在仓库中可以更新到 ...

  9. Spring Cloud 微服务二:API网关spring cloud zuul

    前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...

随机推荐

  1. linux中查看端口号

    linux中查看端口号 yum install lsof -y [root@test1 ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF ...

  2. 使用mousedown、mousemove、mouseup实现拖拽效果

    如何实现一个元素的拖拽效果,使用原生的js实现,习惯了jquery的同学们,你们自己写了吗?N久使用mvvm框架,不写jquery的东西,感觉自己完全不会了. 话不多说,直接上code.本例子以简单的 ...

  3. C++ STL-bitset

    1.bitset的声明 #include <bitset> using std::bitset; 2.bitset对象的定义和初始化       可以如下声明一个该类型变量: bitset ...

  4. R去掉含有NA的行

    只要数据框中含有NA的行都去掉 final[complete.cases(final),] na.omit(final) 过滤某几列 final[complete.cases(final[,5:6]) ...

  5. Ribbon学习笔记

    微服务的概念: Ribbon默认的是轮询的算法:   @LoadBalanced @EnableEurekaClient Irule是根据 Ribbon默认(轮询)的7中负载均衡的算法: 修改默认的R ...

  6. 【剑指offer】面试题 52. 两个链表的第一个公共结点

    面试题 52. 两个链表的第一个公共结点 NowCoder 题目描述 输入两个链表,找出它们的第一个公共结点. Java 实现 ListNode Class class ListNode { int ...

  7. TensorSpace:超酷炫3D神经网络可视化框架

    TensorSpace:超酷炫3D神经网络可视化框架 TensorSpace - 一款 3D 模型可视化框架,支持多种模型,帮助你可视化层间输出,更直观地展示模型的输入输出,帮助理解模型结构和输出方法 ...

  8. 关于/etc/rc.local

    /etc/rc.d/rc.local 用于添加开机启动命令 /etc/rc.local是/etc/rc.d/rc.local的软连接 简单来说 开机自启的

  9. 《Mysql - Order By 的工作原理?》

    一:概述 - order by 用于 SQL 语句中的排序. - 以  select city,name,age from t where city='杭州' order by name limit ...

  10. 往List集合循环add(对象)得到的是重复对象

    记录每次的错误,强大是慢慢的积累,先看看代码, 往list中循环添加RoleKungFu对象,看似没有问题,结果打印则显示: 全部是重复的数据!这是因为啥呢,因为将对象add入list中时,放入lis ...