springcloud13---zuul
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的更多相关文章
- Netflix Zuul 了解
Zuul 是提供动态路由,监控,弹性,安全等的边缘服务.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门.Zuul 可以适当的对多个 Amazon Auto Scal ...
- netflix zuul 学习
netflix zuul 是netflix开发的一个EDGE SERVICE. 主要是作为一个API Gateway 服务器,可以实现安全,流量控制等功能. 我看的是1.x的版本,Zuul1.x的实现 ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- springcloud(十):服务网关zuul
前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...
- Spring REST 与 Zuul 代理
http://www.baeldung.com/spring-rest-with-zuul-proxy 作者: Eugen Paraschiv 译者: http://oopsguy.com 1.概述 ...
- Zuul(SpringCloud学习笔记一)
路由是微服务架构中必须(integral )的一部分,比如,"/" 可能映射到你的WEB程序上,"/api/users "可能映射到你的用户服务上," ...
- zuul超时的解决方案
参考http://www.coolxuewang.com/view/10 在zuul的配置文件里增加如下配置: ribbon: ConnectTimeout: 6000 ReadTimeo ...
- Spring Cloud Zuul
新建Spring Boot工程,命名为zuul 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?&g ...
- Spring Cloud Zuul 添加 ZuulFilter
紧接着上篇随笔Spring Cloud Zuul写,添加过滤器,进行权限验证 1.添加过滤器 package com.dzpykj.filter; import java.io.IOException ...
- springCloud zuul网关服务
第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...
随机推荐
- Solr4.0+IKAnalyzer中文分词安装
1.依赖: JDK1.6,Tomcat 5.5,Solr 4.0.0,IKAnalyzer 2012FF Tomcat虽然不是必须,但觉得上生产环境的话,还是得用Tomcat,便于统一管理和监控. T ...
- 【WebService】使用jaxb完成对象和xml的转换
package com.slp.jxmarshaller; /** * Created by sanglp on 2017/2/26. */ public class ClassName { priv ...
- 手机QQ会员H5加速方案——sonic技术内幕
版权声明:本文由况鹰原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/141 来源:腾云阁 https://www.qclou ...
- Kotlin——初级篇(一):最详细的环境搭建
众所周知,Kotlin出来已经良久了.Kotlin有着众多优势,不管是用于Android开发中,还是Java开发,都能缩减很大的代码量,大大提高了工作效率.而小生本人也是才从忙碌的个工作中抽身出来,有 ...
- 移动前端开发viewport
1.viewport的概念 能在移动设备上正常显示那些传统的为桌面浏览器设计的网站宽度 2.css中的1px并不等于移动设备的1px 在iphone3上,一个css像素确实是等于一个屏幕物理像素的.后 ...
- 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树
[BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...
- log buffer space事件(转)
看了这篇文章: Oracle常见的等待事件说明http://database.ctocio.com.cn/tips/38/6669538.shtml 对于Log Buffer Space-日志缓冲空间 ...
- postgresql----表分区
--下面的描述不记得在哪里抄来的了?! 表分区就是把逻辑上一个大表分割成物理上的多个小块,表分区可提供如下若干好处: 1.某些类型的查询性能可以得到极大提升. 2.更新的性能可以得到提升,因为表的每块 ...
- couldn't connect to host
“couldn't connect to host” 这样的错误可能是主机不可到达,或者端口不可到达. ping OK只代表主机可以到达. 端口不可到达可能是由于HTTP 服务器未启动或者监听在其他端 ...
- Spring boot官方文档学习(一)
个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...