spring boot —— Spring-Cloud-Zuul(网关服务getway)

zull 能做路由

快速入门:

一、 引入zuul依赖:

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

二、创建应用主类。使用@EnableZuulProxy注解开启zuul的API网关服务功能:

@EnableZuulProxy
@SpringCloudApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}

三、在applicable.yml 中配置Zuul应用的基本信息:

spring:
   application:
      name : api-gateway
server: 
  port : 5555

这样一个zuul项目就完成了,zuul使用的重点是配置文件

简单的实例配置(不是用eureka):

 

    格式:

    zuul.routes.<route>.path=/api-b-url/**
    zuul.routes.<route>.url=http://localhost:8080

    route : 服务名称可以随便写

当URL中包含/api-b-url/时,会路由到http://localhost:8080服务上

使用eureka:

    zuul.routes.user-service.path=/user-service/**

    zull.routes.user-service.serviceId=user-service

    可以简写为: zuul.routes.user-service=/user-service/**

 

 

服务路由的默认规则

    当我们为spring cloud zuul 构建API网关服务引入spring cloud eureka之后,它为eureka中的每个服务都自动创建一个默认路由规则,这些默认规则的path会使用serviceId配置的服务名作为请求前缀

默认路由规则:

zuul.routes.user-service.path=/user-service/**
zull.routes.user-service.serviceId=user-service

user-service 是用户自定义的服务名(注册到eureka服务上的服务名)

 

下面附上一个比较全的配置文件,以后还会补充, 格式有点乱自行转换:

spring:
   application:
      name : api-gateway
server: 
  port : 5555

# eureka

eureka: 
  client: 
    serviceUrl: 
      defaultZone : http://localhost:1111/eureka/
# 不使用eureka缺省值 true      
#ribbo:
#  eureka:
#    enabled : true
    
# 路由配置
zuul:
#  prefix : /api # 路由前缀, 会在网关上的路由规则上都加上/api,可能会出现BUG谨慎使用
#  ignored-services : * #服务名匹配表达式。定义不自动创建路由的规则。
  ignored-patterns : /**/biz/ba/**,/**/biz/bt/**   #忽略表达式, 符合规则不路由
  routes: 
    biz-service: /biz/**
      


  # route connection
  host:
    max-per-route-connections : 20
    max-total-connections : 200
  # Disable Zuul Filters
  # zuul.<SimpleClassName>.<filterType>.disable=true
#  AccessFilter:
#    pre:
#      disable : true
      



# 请求转发 :  他实现了将符合/api-b-url/**路由规则的请求转发到API网关中以/local为前缀的请求上
#zuul.routes.api-b-url.path=/api-b-url/**
#zuul.routes.api-b-url.url=forward:/local

# 关闭路由前缀(两种)
#zuul.strip-prefix=false
#zuul.routes.api-b.strip-prefix=false


# 创建请求连接的超时时间
#ribbon.ConnectTimeout=3000
#ribbon.ReadTimeout=1000

# 执行性超时时间
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
#ribbon.ConnectTimeout=3000
#ribbon.ReadTimeout=60000

# 执行超时时间要大于创建请求连接超时时间,若路由请求的处理时间超时依赖服务的请求还未响应时,会自动进行重试路由请求

#关闭重试
#zuul.retryable=false #关闭所有重试
#zuul.routes.<route>.retryable=false #关闭指定重试



# ZuulServlet path
#zuul.servlet-path=/zuul

 代码: https://gitee.com/publicman/xcEduService/tree/master/xc-govern-gateway

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘彦青-Yannis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值