SpringCloud zuul 集成
更多干货
- spring cloud 微服务
- spring cloud 知识点
- 服务发现与服务注册
- 定制Rabbon客户端负载均衡策略
- Spring Cloud Feign使用1
- SpringCloud Feign使用二
- SpringCloud Hystrix 实现
- SpringCloud超时机制、断路器模式简介
- Spring Cloud Eureka HA 高可用
- SpringCloud Turbine
- SpringCloud zuul 网关 集成
- SpringCloud Conf 搭建配置中心
- spring cloud- 阿波罗 apllo 本地开发环境
- SpringCloud Conf 配置中心 属性加解密之对称加密
- Spring Cloud 配置中心 认证和高可用
配置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