SpringCloud---SpringCloud Zuul

SpringCloud Zuul简述:

       Zuul是SpringCloud提供的一个网关路由组件。用于对请求的路由和过滤。路由功能负责将外部请求转发到具体的微服务实例上;而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。

SpringCloud Zuul使用思路:

       Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中读取其他微服务实例。这样可以实现通过Zuul路由网关消费微服务实例。

创建module,代码实现:

      pom.xml导入Zuul对应坐标和Eureka客户端坐标

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

       application.properties 

#网关端口
server.port=6060
#次module应用名称
spring.application.name=ZuulGateway

#Eureka集群注册中心
eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/,http://127.0.0.1:7002/eureka/
#此module实例名称
eureka.instance.instance-id=SpringCloudZuul
#是否显示此module所在服务器ip
eureka.instance.prefer-ip-address=true

       启动类添加新注解

@EnableEurekaClient
@EnableZuulProxy
扫描二维码关注公众号,回复: 9574013 查看本文章

测试:

      启动Eureka集群中心,启动微服务实例,启动Zuul网关,访问微服务实例上接口的路径:

http://网关ip:网关端口/Eureka上微服务实例应用名称/微服务实例中接口的具体路径

Zuul网关拓展:

  拓展1:

        因为安全性,不想在url中暴漏微服务实例的应用名称。可以在Zuul module的配置文件加入如下配置:

#配置自己的路由规则,将路径中微服务应用名称microservicecloud-dept用gateway代替
zuul.routes.mydept.serviceId=microservicecloud-dept
zuul.routes.mydept.path=/gateway/**

zuul.routes.userService.serviceId=microservicecloud-user
zuul.routes.userService.path=/user/**

         访问微服务实例接口路径如下:

http://网关ip:网关端口/gateway/微服务实例中接口的具体路径

http://网关ip:网关端口/user/微服务实例中接口的具体路径

拓展2:

        禁止使用Zuul默认路由配置,即禁用含有微服务应用名称的路径,添加如下配置。

#禁用Zuul默认路由规则,只认我们自己配置的路由规则。如果有多个,用逗号分隔。
zuul.ignoredServices=microservicecloud-dept,microservicecloud-user

        全部禁止:

#禁用Zuul默认路由规则,只认我们自己配置的路由规则。对Eureka所有微服务起作用
zuul.ignoredServices=*

       

发布了66 篇原创文章 · 获赞 26 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39115469/article/details/104646868