API Gateway

  服务网关在微服务中是一个很重要的组成部分,通过服务网关可以统一向外提供REST API,例如 / 映射到后端应用 /api/user 映射到 user service,  /api/comment 映射到comment service,在spring cloud中通过集成zuul来实现这个 gateway,他提供一下功能

    • Authentication
    • Insights
    • Stress Testing
    • Canary Testing
    • Dynamic Routing
    • Service Migration
    • Load Shedding
    • Security
    • Static Response handling
    • Active/Active traffic management

  

  我们使用两个服务,我们以user-provider-service来进行测试

  我们访问http://localhost:8081/users 可以看到如下输出,8081 就是user-provider-service服务的端口

 
[{"id":"1","name":"陈七","tel":null},{"id":"2","name":"王六","tel":null}]

  

  建一个api-gateway module

  pom文件

  

  <parent>
<groupId>com.dh.cloud</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <dependencies> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-sidecar</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency> </dependencies>

 启动类

 

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

application.yml

server:
port: 10000 sidecar:
port: 8000 endpoints:
restart:
enabled: true
shutdown:
enabled: true
health:
sensitive: false eureka:
instance:
hostname: gateway
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/

bootstrap.yml

 

spring:
application:
name: api-gateway
cloud:
config:
uri: http://localhost:8888
encrypt:
failOnError: false

这里使用的都是默认配置,运行代码

访问http://localhost:10000/user-provider-service/users,10000为gateway的端口

看到输出

[{"id":"1","name":"陈七","tel":null},{"id":"2","name":"王六","tel":null}]

和直接访问服务一致,到此zuul一个简单的演示就完成了

spring-cloud 学习四 服务网关的更多相关文章

  1. spring cloud学习笔记五 网关服务zuul

    网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用.   网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...

  2. spring cloud 学习之路由网关(zuul)

    学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...

  3. spring cloud 学习之服务消费者(rest+ribbon)

    学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...

  4. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  5. spring cloud学习(一) 服务注册

    首先spring-cloud相关的简介可以去百度搜索,这里就不多说了,这里分享一个翻译spring cloud官网的中文网站spring cloud中文网 这个学习项目的代码放在 https://gi ...

  6. Spring Cloud(十):服务网关zuul(转)

    前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...

  7. spring cloud学习(四) Fegin 的使用

    Feign 的使用 什么是Feign? Feign : Declarative REST clients. Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创 ...

  8. SpringCloud学习笔记(20)----Spring Cloud Netflix之服务网关Zuul的各种姿势

    1. 禁用过滤器 # zuul.<SimpleClassName>.<filterType>.disable=true # 例如禁用 自定义的过滤器 zuul.MyFilter ...

  9. SpringCloud学习笔记(18)----Spring Cloud Netflix之服务网关Zuul原理

    1. Zuul的工作机制 Zuul提供了一个框架,可以对过滤器进行动态的加载,编译,运行.过滤器之间没有直接的相互通信,他们是通过一个RequestContext的静态类来进行数据传递的.Requet ...

随机推荐

  1. pwn学习日记Day21 《程序员的自我修养》读书笔记

    Linux内核装载ELF过程 (1)bash进程调用fork()系统调用创建一个新的进程 (2)新的进程调用execve()系统调用执行指定的ELF文件,原先的bash进程继续返回等待刚才启动的新进程 ...

  2. Mybatis按照SQL查询字段的顺序返回查询结果,使用resultType="java.util.LinkedHashMap"

    在使用Mybatis开发时,Mybatis返回的结果集就是个map,当返回map时只需要做好SQL映射就好了,减少了代码量,简单便捷,缺点是不太方便维护,但是写大量的vo类去返回也挺累的,这个看你个人 ...

  3. tcpdump抓包文件分段保存-指定时间或者指定大小

    2018年09月06日 15:36:11 天已青色等烟雨来 阅读数:3628    版权声明:博客地址:blog.csdn.net/x356982611,未经允许不得转载,不得转载,不得转载 http ...

  4. Vue于React特性对比(三)

    最近重学React,再次和vue做了对比. 一,为官方插件提供便利的第三方插件横行 React仅仅是一个ui框架.虽然官方提供了redux,react-router:但也有第三方的redux-thun ...

  5. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  6. Apache损坏无法使用怎么办

    已经 find / -name httpd | xargs rm -rf删光了httpd相关文件,但是使用yum install httpd 无法正常安装. 查看Httpd的状态是 解决办法: yum ...

  7. CPU密集型、IO密集型

    CPU密集型(CPU-bound) CPU密集型也叫计算密集型,指的是系统的硬盘.内存性能相对CPU要好很多,此时,系统运作大部分的状况是CPU Loading 100%,CPU要读/写I/O(硬盘/ ...

  8. 修改ssh端口

    centos7.5修改默认SSH端口 linux SSH默认端口是22,不修改的话存在一定的风险,要么是被人恶意扫描,要么会被人破解或者攻击,所以我们需要修改默认的SSH端口 1.修改22端口为620 ...

  9. mysql查看数据库所占用的空间

    查询某个表所占用的磁盘空间大小: SELECT CONCAT(ROUND(SUM(data_length/1024/1024),2),'MB') AS data_length_MB, CONCAT(R ...

  10. SpringBoot: 13.异常处理方式3(使用@ControllerAdvice+@ExceptionHandle注解)(转)

    问题:使用@ExceptionHandle注解需要在每一个controller代码里面都添加异常处理,会咋成代码冗余 解决方法:新建一个全局异常处理类,添加@ControllerAdvice注解即可 ...