Zuul:API  GATEWAY (服务网关):

http://blog.daocloud.io/microservices-2/

一个客户端不同的功能请求不同的微服务,那么客户端要知道所有微服务的ip和端口,如果有的微服务不是rest协议是用的别的协议,有时候有可能几个微服务要合并,上面都是问题。

所以前面要加一个服务网关,客户端只需要知道网关的ip和端口就可以了,网关知道后面微服务的ip和端口,缺点是要考虑网关的高可用。

Ribbon是客户端的负载均衡器,Zuul是服务端的负载均衡器。

使用http://192.168.88.1:7901/simple/1直接访问user微服务(7901是user的微服务地址),

http://192.168.88.1:8040/microservice-provider-user/simple/1(8040是zuul的端口,microservice-provider-user是user微服务的application:name)。通过zuul就可以访问user服务了。

给user微服务指定别名。默认代理所有注册到eureka上的微服务。

一个工程只有src和pom文件,加入.project文件然后更新工程就会出现.classpath文件和其他的文件就可以跑起来了。

经过zuul的请求都会通过hysitrcs包裹,所以zuul会有断路器功能。zuul还使用了ribbon做负载均衡。

Zuul过滤器:

Zuul有4中过滤器,PRE,ROUTING,POST,ERROR。Pre先执行然后routing,然后post然后error.

pre是zuul请求别的微服务之前,routing是请求过程中的,post是请求到微服务之后可以添加一些header,error是抛异常了。

也可以自定义过滤器。

周立springclud : http://www.itmuch.com/advertisment/my-spring-book/

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableZuulProxy //使用这一个注解就可以注册到eureka,
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
routes:
abc: # abc随意,只要唯一就可以
path: /user-path/** # 使用user-path访问user微服务
serviceId: microservice-provider-user # 注册到eureka的服务的application名字 # http://localhost:8040/routes : 查看zuul代理的微服务
# {"/user-path/**":"microservice-provider-user","/microservice-provider-user/**":"microservice-provider-user"}
# 就可以使用zuul来代理user微服务:http://localhost:8040/microservice-provider-user/simple/1
# zuul的请求都用hystrix包裹:http://localhost:8040/hystrix.stream
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
prefix: /simple
strip-prefix: false
logging:
level:
com.netflix: debug
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka: #注册到eureka server上面去
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
#prefer-ip-address: false # localhost:microservice-gateway-zuul:8040 #经过zuul的请求都会通过hysitrcs包裹,配置hysitrcs的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000 #zuul还使用了ribbon做负载均衡,要设备ribbon的超时时间
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
prefix: /api # http://192.168.88.1:8040/api/microservice-provider-user/simple/1 前缀加服务的名称
strip-prefix: true # 为false就不能通过加前缀/api来访问了,
logging:
level:
com.netflix: DEBUG
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
routes:
abc:
path: /user-url/**
url: http://192.168.85.1:7900/
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
routes:
abc: #只针对abc路由
path: /user-url/**
service-id: microservice-provider-user
ribbon:
eureka:
enabled: false
# http://192.168.88.1:8040/microservice-provider-user/simple/1 会从7901和7902这2个节点来请求
microservice-provider-user: # 这边是ribbon要请求的微服务的serviceId,7901和7902是2个user微服务,
ribbon:
listOfServers: http://localhost:7901,http://localhost:7902
spring:
application:
name: microservice-gateway-zuul
server:
port: 8040
eureka:
client:
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
zuul:
ignoredServices: microservice-consumer-movie-ribbon-with-hystrix #不想反向代理microservice-consumer-movie-ribbon-with-hystrix微服务
routes:
microservice-provider-user: /user/** #user微服务的别名
# 现在http://192.168.88.1:8040/user/simple/1访问user微服务,原来http://192.168.88.1:8040/microservice-provider-user/simple/1
#默认zuul会反向代理所有注册到eureka的微服务
<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> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>microservice-gateway-zuul</artifactId>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- 依赖,还要加eureka client的依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies> </project>

springcloud13---zuul的更多相关文章

  1. Netflix Zuul 了解

    Zuul 是提供动态路由,监控,弹性,安全等的边缘服务.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.Zuul 可以适当的对多个 Amazon Auto Scal ...

  2. netflix zuul 学习

    netflix zuul 是netflix开发的一个EDGE SERVICE. 主要是作为一个API Gateway 服务器,可以实现安全,流量控制等功能. 我看的是1.x的版本,Zuul1.x的实现 ...

  3. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  4. springcloud(十):服务网关zuul

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

  5. Spring REST 与 Zuul 代理

    http://www.baeldung.com/spring-rest-with-zuul-proxy 作者: Eugen Paraschiv 译者: http://oopsguy.com 1.概述 ...

  6. Zuul(SpringCloud学习笔记一)

    路由是微服务架构中必须(integral )的一部分,比如,"/" 可能映射到你的WEB程序上,"/api/users "可能映射到你的用户服务上," ...

  7. zuul超时的解决方案

    参考http://www.coolxuewang.com/view/10 在zuul的配置文件里增加如下配置: ribbon:    ConnectTimeout: 6000    ReadTimeo ...

  8. Spring Cloud Zuul

    新建Spring Boot工程,命名为zuul 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?&g ...

  9. Spring Cloud Zuul 添加 ZuulFilter

    紧接着上篇随笔Spring Cloud Zuul写,添加过滤器,进行权限验证 1.添加过滤器 package com.dzpykj.filter; import java.io.IOException ...

  10. springCloud zuul网关服务

    第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...

随机推荐

  1. Unity中SendMessage和Delegate效率比较

    网上直接搜的代码.需要的使用也简单,所以就不过多说明. 但是网上都说,他们之间的差距,delegate比较快,效果高.怎么个高法呢?还是自己来测试下时间. 故此, 个人之用来比较下时间差别. 一.直接 ...

  2. 事件和winform的学习

             记得现在已经不在学习winform啦,可是我们为什么还是学习啦,我感觉就是帮助我们往下一个层次进发啦,因为从控制台直接开始进入webform的学习,我们很难接受啦,估计效率也不高啦, ...

  3. tomcat源码---->request的请求参数分析

    当contentType为application/json的时候,在servlet中通过request.getParameter得到的数据为空.今天我们就java的请求,分析一下request得到参数 ...

  4. Angular基础---->AngularJS的使用(一)

    AngularJS主要用于构建单页面的Web应用.它通过增加开发人员和常见Web应用开发任务之间的抽象级别,使构建交互式的现代Web应用变得更加简单.今天,我们就开始Angular环境的搭建和第一个实 ...

  5. Elasticsearch 常用基本查询

    安装启动很简单,参考官网步骤:https://www.elastic.co/downloads/elasticsearch 为了介绍Elasticsearch中的不同查询类型,我们将对带有下列字段的文 ...

  6. log4j和commons- logging(好文整理转载)

    一 :为什么同时使用commons-logging和Log4j?为什么不仅使用其中之一? Commons-loggin的目的是为 “所有的Java日志实现”提供一个统一的接口,它自身的日志功能平常弱( ...

  7. 【BZOJ4099】Trapped in the Haybales Gold STL

    [BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...

  8. Sql Server 关于SET IDENTITY_INSERT的问题

    想要将值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 示例: 1.首先建立一个有标识列的表: CREATE TABLE products ( ...

  9. 阿里云搭建SVN服务器

    1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如下内容: wk ...

  10. Yii 的session 实现返回上上页面

    学习session的页面:http://www.yiichina.com/doc/guide/2.0/runtime-sessions-cookies 关键摘要: $session = Yii::$a ...