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>