SpringCloud zuul 网关 集成

SpringCloud zuul 集成

更多干货

配置zuul

pom.xml 配置依赖

  • 引入 zuul
  • 引入eureka客户端
		<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>

配置应用路口 EnableZuulProxy 是组合注解 已经包含 euraka客户端。

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }
}

配置文件

eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

访问注册的应用

  • microservice-consumer-movie-feign-with-hystrix 为其中反向代理的应用的application name
http://localhost:8040/microservice-consumer-movie-feign-with-hystrix/movie/1

配置说明

  • zuul.routes 下可以给指定的微服务配置别名
  • zuul 默认代理euraka上的所有微服务
zuul:
  routes:
    microservice-consumer-movie-feign-with-hystrix: /user/**

以下配置表示只代理配置的微服务

  • ignoredServices * 表示不代理所有的服务
  • zuul
zuul:
  ignoredServices: '*'
  routes:
    microservice-consumer-movie-feign-with-hystrix: /user/**

  • 为微服务取别名
zuul:
  routes:
    abc:
      path: /user-path/**
      serviceId: microservice-provider-user

不实用euraka 实现负载均衡

ribbon:
  eureka:
    enabled: false
microservice-provider-user:     # 这边是ribbon要请求的微服务的serviceId
  ribbon:
    listOfServers: http://localhost:7900,http://localhost:7901

实现安装版本号代理

  • zuul入口类中注入 serviceRouteMapper bean
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }

  @Bean
  public PatternServiceRouteMapper serviceRouteMapper() {
    return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
  }
}
  • 微服服务提供者的appname 中加入版本信息
server:
  application:
    name: microservice-provider-user-v1
  • 通过zuul访问微服务 localhost:8048/v1/movie/1

  • 访问代理的微服务都加上api前缀
zuul:
  prefix: /api

  • 微服务提供者的接口都以api开头,使用zuul代理可以不加api。做以下配置后默认加上
  • 以下是全局的配置。可以配置局部
zuul:
  prefix: /api
  strip-prefix: false

解决zuul上传文件超时

  • zuul默认使用hystrix 所以要设置hystrix超时时间
  • zuul 使用ribbon 所以设置ribbon的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

增加jvm的堆内存

-Xms512M -Xmx1024M
  • 使用curl 上传文件
curl -F "file=@test.txt" localhost:8085/upload
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值