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. centos7中oracle数据库安装和卸载

    参考: 完全命令行安装(验证可行):https://jingyan.baidu.com/article/90895e0f29c92164ec6b0bd1.html 存在疑问:是否需要jdk的配置(因为 ...

  2. 重读APUE(6)-umask

    umask函数设置当前进程的权限为屏蔽字:系统会有一个默认的屏蔽字,为了确保创建文件具有的权限位成功被设置,需要使用umask将屏蔽字置0: 系统屏蔽字用shell查看,比如得到如下结果,其为八进制表 ...

  3. docker-compose ELK+Filebeat查看docker及容器的日志

    我目前所在公司开发团队比较小,为集团下面的工厂开发了一套小的系统,跑在一台CentOS服务器上,服务器搭建了docker环境,安装了docker-compose,但在日志处理方面,暂时没有一个好的方法 ...

  4. 如何把Java的double类型变量保留两位小数

    已知 双精度标量 f,  如果想以字符串形式输出,小数点后保留2位,可直接通过C语言的输出格式,System.out.printf("%.2f", f), 达到目的. 如果想要先转 ...

  5. 黑马vue---61、为什么vue组件的data要是一个函数

    黑马vue---61.为什么vue组件的data要是一个函数 一.总结 一句话总结: 因为js中以函数为变量作用域,所以这样可以保证每个组件的数据不互相影响 二.why components data ...

  6. Oracle常用操作表结构的语句

    首先,一起来认识几个单词. alter (改变) rename(重命名) column(柱子,用来表示列) modify(修改) comment on (评论) truncate (删减,截断) 1. ...

  7. 安装mysql问题解决

    [root@apollo init.d]# /etc/init.d/mysqld startmy_print_defaults: Can't read dir of '/etc/my.cnf.' (E ...

  8. Win7 双系统安装Centos7,并由windows引导程序引导

    1. 在windows磁盘管理中,压缩卷,腾出40G,需保证一个磁盘设备最多只有3个主分区2. 网上下载centos7的dvd.iso3. 使用UltraISO刻录到U盘4. 重启系统F12使用usb ...

  9. 网页是如何实现从剪贴板从读取图片并上传到server的

    代码比较简单,原理更简单,不多言请直接看代码. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  10. ajax提交文件,django测试脚本环境书写,froms组件,钩子函数

    1.在新版本中,添加app是直接在settings设置中,将INSTALLED_APPS里添加app名字, 但是他的完整写法是   'app01.apps.App01Config'  因为新版本做了优 ...