Consul集群加入网关服务

架构示意图



外部的应用或网站通过外部网关服务消费各种服务,内部的生产者本身也可能是消费者,内部消费行为通过内部网关服务消费。

一个内部网关和一个外部网关以及一个Consul Client部署在一台服务器上,这样的网关服务器至少2组,外部网关前面还会有负载均衡设备,内部网关服务使用Consul Client进行查询后使用,内部网关的负载均衡由Consul负责了。

搭建演示环境

Consul集群Server+Client模式的基础上,我们更新并启动网关服务和消费者服务,演示环境中我们只启动一个网关服务进行模拟。

删除spring-cloud-gateway和spring-cloud-consul-consumer这两个容器。

docker pull bluersw/spring-cloud-gateway:v3

docker run --name=spring-cloud-gateway -d -p 9000:9000 bluersw/spring-cloud-gateway:v3 /opt/consul/./consul agent -data-dir=/opt/consul/data -config-dir=/opt/consul/config -node=gw-cc  -join 172.17.0.2

docker exec spring-cloud-gateway  /usr/local/java/bin/java -jar /opt/spring-cloud-gateway-0.0.1-SNAPSHOT.jar

docker pull bluersw/spring-cloud-consul-consumer:v3

docker run --name=spring-cloud-consul-consumer -d -p 9003:9003 bluersw/spring-cloud-consul-consumer:v3  /opt/consul/./consul agent -data-dir=/opt/consul/data -config-dir=/opt/consul/config -node=consumer-cc  -join 172.17.0.2

docker exec  spring-cloud-consul-consumer /usr/local/java/bin/java -jar /opt/spring-cloud-consul-client-0.0.1-SNAPSHOT.jar



TAG:V3版本的网关和消费者镜像修改内容

spring-cloud-gateway的项目配置文件修改如下(也是在本机Consul Client注册),主要是为了增加prefer-ip-address否则Consul获取不到服务的IP地址:

server:
port: 9000
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
register: true
prefer-ip-address: true
health-check-path: /actuator/health
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
- name: Hystrix
args:
name: service-provider-fallback
fallbackUri: forward:/service-provider-error
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY,BAD_REQUEST
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/default-error
application:
name: PC-ApiGateWay

为了模拟内部服务调用网关消费其他服务,spring-cloud-consul-client项目(spring-cloud-consul-consumer)添加如下代码:

创建Feign风格的代理类

//网关服务
@FeignClient(name="PC-ApiGateWay")
public interface GatewayRemote { //网关上的请求地址和外部用浏览器浏览的路径相同
@RequestMapping("/service-provider/hello")
public String Hello(@RequestParam String name); }

Controller里增加如下方法:

    @Autowired
GatewayRemote gatewayRemote; @RequestMapping("/TestGW")
public String TestGW(){
String first = gatewayRemote.Hello("first-SWS");
String second = gatewayRemote.Hello("second-SWS");
return first + " | " + second;
}

模拟外部访问

直接在浏览器里访问127.0.0.1:9000/service-provider/hello?name=sws,得到服务的返回信息:

模拟内部访问

在浏览器里访问127.0.0.1:9003/TestGW,得到服务的返回信息:

源码

Github仓库:https://github.com/sunweisheng/spring-cloud-example

Consul集群加入网关服务(Spring Cloud Gateway)的更多相关文章

  1. 网关服务Spring Cloud Gateway(二)

    上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...

  2. 网关服务Spring Cloud Gateway(三)

    上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能. 修改请求路径的过滤器 StripPrefi ...

  3. 网关服务Spring Cloud Gateway(一)

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

  4. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  5. .net core下,Ocelot网关与Spring Cloud Gateway网关的对比测试

    有感于 myzony 发布的 针对 Ocelot 网关的性能测试 ,并且公司下一步也需要对.net和java的应用做一定的整合,于是对Ocelot网关.Spring Cloud Gateway网关做个 ...

  6. api网关揭秘--spring cloud gateway源码解析

    要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...

  7. 最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

    前言: 最近开发了Zuul网关的实现和Spring Cloud Gateway实现,对比Spring Cloud Gateway发现后者性能好支持场景也丰富.在高并发或者复杂的分布式下,后者限流和自定 ...

  8. 创建网关项目(Spring Cloud Gateway)

    创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <s ...

  9. spring Cloud网关之Spring Cloud Gateway

    Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...

随机推荐

  1. php内置函数分析之current()、next()、prev()、reset()、end()

    current()初始指向插入到数组中的第一个单元 next() 将数组的内部指针向前移动一位 prev() 将数组的内部指针倒回一位 reset() 将数组的内部指针指向第一个单元 end() 将数 ...

  2. vi编辑器的快捷键汇总

    光标控制命令 本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027& ...

  3. Python---进阶---文件操作---获取文件夹下所有文件的数量和大小

    一.####编写一个程序,统计当前目录下每个文件类型的文件数 ####思路: - 打开当前的文件夹 - 获取到当前文件夹下面所有的文件 - 处理我们当前的文件夹下面可能有文件夹的情况(也打印出来) - ...

  4. js 将时间戳转为日期格式

    最近项目需要在前端将一个13位的时间戳显示成日期格式,在网上查了很多都不符合要求,只有一个是能满足要求的,在这记录一下,说不定以后还用的着. 13位时间戳改为yyyy-MM-dd HH-mm-ss 格 ...

  5. CSS背景和CSS3背景background属性

    css背景属性用于定义HTML元素的背景 背景属性既可以为单个的单元设置背景,也可以为整个页面设置背景,可以对上述二者的任意组合设置背景,段落.文字.不同状态的链接.图像.内容区域修改其背景样式.设置 ...

  6. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  7. Spring——框架

    [定义] 框架就是制定一套规则或规范(思想),大家(程序员)在该规范或者规则(思想)下工作. 或者说是使用别人搭好的舞台,你来表演. [特点] ——半成品 ——封装了特定的处理流程和控制逻辑 ——成熟 ...

  8. iOS-Swizzle

    最后更新:2017-06-21 一.先说结论 void swizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector) { M ...

  9. Python 元组遍历排序操作方法

    在Python不可变数据类型中,有一个比较重要的角色那就是元组( tuple ).如果某个对像被定义为元组类型,那么就意味着它的值不能被修改,除非重新定义一个新的对像.元组和List列表常被放在一起进 ...

  10. postgresql批量删除表

    CREATE FUNCTION del_ora_table() RETURNS void AS $$ DECLARE tmp ); DECLARE names CURSOR FOR select ta ...