SpringCloud Zuul实现负载均衡和熔断机制

一、场景

笔者就Zuul网关下实现其负载均衡与熔断机制(雪崩)进行实践,前提是已经导入zuul相关依赖

springboot版本:1.5.9.RELEASE

springcloud版本:Dalston.SR5


        
            org.springframework.cloud
            spring-cloud-starter-zuul
        
        
            com.netflix.zuul
            zuul-core
            1.3.0
        

二、场景实现

1、在网关的配置文件中配置ribbon(负载均衡)和hystrix(熔断机制)

#熔断机制
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000

#负载均衡
ribbon:
  ConnectionTimeout: 500
  ReadTimeout: 2000
#端口
server:
  port: 8080
spring:
  #该配置文件中的配置,对应的服务名称是wc-gateway
  application:
    name: wc-gateway
  profiles:
    active: dev
#服务网关配置
zuul:
  host:
    connect-timeout-millis: 60000
    socket-timeout-millis: 60000
  #路由规则
  routes:
    api:
      path: /api/user/**
      serviceId: wc-client-user

其实ribbon的真实值=(ConnectionTimeout+ReadTimeout)*2,该值最好小于hystrix的timeoutInMilliseconds的值,因为如果大于其值会失去负载均衡(ribbon)的重试机会,而直接熔断

2、验证负载均衡

因为zuul下自带了hystrix,ribbon相关jar包,所有现在已经实现了负载均衡和熔断机制,接下来进行验证

在client服务下编写controller,测试负载均衡

package top.wingcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: linjie
 * @description: 用户服务请求处理控制器
 * @create: 2018/11/06 09:16
 */
@RestController
public class UserController {
    @Value("${server.port}")
    private int port;

    @RequestMapping("index")
    public String index(){
        return "Hello World!"+port;
    }
}

依次启动注册中心、配置中心、client服务、修改端口再次启动client服务、服务网关

根据网关的路由,访问同一个路由,发现启动的两个不同端口的client服务交替执行

出现该情况即实现了负载均衡

3、验证熔断机制

在网关服务中需要写ZuulFallbackProvider的实现类

package top.wingcloud.filter;

import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: linjie
 * @description:错误拦截回显,熔断
 * @create: 2018/10/11 20:01
 */
@Component
public class ApiFallbackProvider implements ZuulFallbackProvider{

    @Override
    public String getRoute() {
        //设置熔断的服务名
        //如果是所有服务则设置为*
        return "wc-client-user";
    }

    @Override
    public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return 200;
            }

            @Override
            public String getStatusText() throws IOException {
                return "{code:0,message:service error =_=}";
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream(getStatusText().getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

这个时候关闭client所有服务,再次访问之前的网关路由

出现了getStatusText()中的提示,即实现了熔断机制

好了,zuul下的负载均衡和熔断已经实现,时间不早了,赶紧睡觉!

你可能感兴趣的:(●,Spring全家桶)