SpringCloud服务治理(二)网关高可用

429 阅读1分钟

总体设计架构图

1,nginx负载均衡

http {
    upstream tomcats {
        server localhost:19997;
        server localhost:19998;
        server localhost:19999;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://shopservice.api;
        }
    }
}

2,产品服务配置请求60s内不超过三次

<dependency>
    <groupId>com.marcosbarbero.cloud</groupId>
    <artifactId>spring-cloud-zuul-ratelimit</artifactId>
    <version>1.3.4.RELEASE</version>
</dependency>

zuul.ratelimit.enabled=true
zuul.routes.api-a.id=api-a
zuul.routes.api-a.path=/api/a/**
zuul.routes.api-a.service-id=productservice
zuul.ratelimit.api-a.limit=3
zuul.ratelimit.api-a.refresh-interval=60
zuul.ratelimit.api-a.type=origin

限流参数:

3,zuul网关超时重试

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.3.1</version>
</dependency>

# 开启zuul网关重试
zuul.retryable=true
# hystrix超时时间设置
# 线程池隔离,默认超时时间1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=8000
# ribbon超时时间设置:建议设置比Hystrix小
# 请求连接的超时时间: 默认5000ms
ribbon.ConnectTimeout=5000
# 请求处理的超时时间: 默认5000ms
ribbon.ReadTimeout=5000
# 重试次数:MaxAutoRetries表示访问服务集群下原节点(同路径访问);MaxAutoRetriesNextServer表示访问服务集群下其余节点(换台服务器)
ribbon.MaxAutoRetries=1
ribbon.MaxAutoRetriesNextServer=1
# 开启重试
ribbon.OkToRetryOnAllOperations=true