Zuul的基本应用,反向代理

Zuul是一个Server端的路由器和过滤器,Zuul默认的会反向代理到所有注册到Eureka上的服务。

Zuul实现代理。

启动类

[java]  view plain  copy
  1. package com.dynamic.cloud;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;  
  6.   
  7. @SpringBootApplication  
  8. @EnableZuulProxy  
  9. public class ZuulApplication   
  10. {  
  11.     public static void main( String[] args )  
  12.     {  
  13.         SpringApplication.run(ZuulApplication.class, args);       
  14.     }  
  15. }  

后面的代码中,启动类不会变化。

  @EnableZuulProxy,是一个组合注解,@EnableCircuitBreaker,@EnableDiscoveryClient。因为它有@EnableDiscoveryClient,所以可以注册到Eureka上。

POM.xml中加入依赖

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <artifactId>microservice-gateway-zuul</artifactId>  
  6.   <packaging>jar</packaging>  
  7.   
  8.     <parent>  
  9.             <groupId>com.dynamic.cloud</groupId>  
  10.             <artifactId>microservice-spring-cloud</artifactId>  
  11.             <version>0.0.1-SNAPSHOT</version>  
  12.     </parent>  
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   
  18. <dependencies>  
  19.     <dependency>  
  20.         <groupId>org.springframework.cloud</groupId>  
  21.         <artifactId>spring-cloud-starter-eureka</artifactId>  
  22.     </dependency>  
  23.     <dependency>  
  24.         <groupId>org.springframework.cloud</groupId>  
  25.         <artifactId>spring-cloud-starter-zuul</artifactId>  
  26.     </dependency>  
  27.   
  28. </dependencies>  
  29.   
  30.   
  31. </project>  
配置文件application.yml
[html]  view plain  copy
  1. spring:  
  2.   application:  
  3.     name: microservice-gateway-zuul  
  4. server:  
  5.   port: 8040   
  6. eureka:  
  7.   client:   
  8.     service-url:   
  9.       defaultZone: http://user:pass123@localhost:8761/eureka #把Eureka註冊到那個Eureka  
  10.   instance:   
  11.     prefer-ip-address: true  
  12.     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}  
  13. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000  
  14. zuul:  
  15.   ignored-services: microservice-comsumer-movie-ribbon-withhystrix  
  16.   routes:  
  17.     microservice-provider-user: /user/**  
  18.       

zuul:

  ignored-services: microservice-comsumer-movie-ribbon-withhystrix  ,不允许代理本服务
  routes:
    microservice-provider-user: /user/**   ,可以使用user替换microservice-provider-user访问路径。

只想反向代理一个微服务,不想反向代理其他的微服务

第一种:所有的服务都ignored,只有配置的才反向代理
第二种:在ignored上配置,不代理的服务。

启动Eureka,用户服务,Zuul服务




已经完成了简单的Zuul的实例


1.指定path+serviceid


[html]  view plain  copy
  1. zuul:   
  2.   routes:   
  3.     abc: #让zuul方向代理微服务,路径是/user-path  abc只要是唯一的就行,可随意写  
  4.       path: /user-path/**  #封层匹配  
  5.       serviceId: microservice-provider-user  
  6.       


2.指定Path+url


[html]  view plain  copy
  1. zuul:   
  2.   routes:   
  3.     abc: #让zuul方向代理微服务,路径是/user-path  abc只要是唯一的就行,可随意写  
  4.       path: /user-path/**  #封层匹配  
  5.       #serviceId: microservice-provider-user  
  6.       url: http://localhost:7900/  
使用path + url不能实现服务提供者的负载均衡。

3.解决path + url 实现负载均衡


[html]  view plain  copy
  1. zuul:   
  2.   routes:   
  3.     abc: #让zuul方向代理微服务,路径是/user-path  abc只要是唯一的就行,可随意写  
  4.       path: /user-path/**  #封层匹配  
  5.       serviceId: microservice-provider-user  
  6. ribbon:  
  7.   eureka:  
  8.     enabled: false  
  9. microservice-provider-user: #这是Ribbon要请求的微服务的ServiceId  
  10.   ribbon:  
  11.     listOfServers: http://localhost:7900,http://localhost:7901  
  Zuul是API 网关解决微服问题的实现,反向代理,默认情况下,可以代理所有注册在Eureka上面的服务,实现在服务端的负载均衡。

猜你喜欢

转载自blog.csdn.net/justlpf/article/details/80352637
今日推荐