Spring-Cloud-Zuul(网关服务)

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

猜你喜欢

转载自blog.csdn.net/ityqing/article/details/79481490
今日推荐