spring cloud zuul 配置

 

参考:http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html

 

spring boot版本:2.0.3.RELEASE

 

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- 通过注册中心调用时需要引入此依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>

 

application.properties

可以手动配置固定地址调用,也可以通过注册中心调用,二选一

spring.application.name=gateway-service-zuul-wly
server.port=8088
#1.显式将所有/wly/**请求转发给http://localhost:8081处理,例如/wly/a等同于http://localhost:8081/a
#zuul.routes.wly.path=/wly/**
#zuul.routes.wly.url=http://localhost:8081
#2.配置注册中心地址,默认通过注册中心调用,规则“/注册中心serviceId/xxx”
eureka.client.serviceUrl.defaultZone=http://localhost:8765/eureka/

 

启动类

package com.wang.springcloudzuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class SpringcloudzuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudzuulApplication.class, args);
    }
}

 

启动一个注册中心、1个生产者、2个消费者、1个zuul服务,如下图:

 

单独访问消费者服务

http://localhost:8081/a/c?name=sb

页面

Hello World!sb

 

使用zuul通过eureka多次访问消费者服务

http://localhost:8088/spring-cloud-consumer/a/c?name=sb

页面

Hello World!sb

查看控制台日志

多次请求zuul的服务,请求会被分发到不同的消费者服务里

 

zuul服务熔断机制

新增一个类

package com.wang.springcloudzuul.fallback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
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 wly
 * @Date 2018/7/3 15:53
 */
@Component
public class ProducerFallBack implements FallbackProvider {
    private final Logger logger = LoggerFactory.getLogger(FallbackProvider.class);

    /**
     * 定义对那个服务做熔断
     * @return
     */
    @Override
    public String getRoute() {
        return "spring-cloud-provider";
    }

    /**
     * 提供者路由熔断返回值
     *
     * @return
     */
    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 "OK";
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("The service is unavailable.".getBytes());
            }

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

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        if (cause != null && cause.getCause() != null) {
            String reason = cause.getCause().getMessage();
            logger.info("Excption {}", reason);
        }
        return fallbackResponse();
    }
}

 

当服务调用失败,会返回

The service is unavailable.

 

zuul服务重连机制

pom.xml新增配置

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

 

applicaiton.yml新增配置

#服务重试功能
zuul.retryable=true
#重试次数
ribbon.MaxAutoRetries=2
#重试其他实例的最大次数
ribbon.MaxAutoRetriesNextServer=0

 

提供者controller

package com.wang.eurekaprovider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wly
 * @Date 2018/6/27 16:15
 */
@RestController
@RequestMapping("/a")
public class HelloController {
    @RequestMapping("/b")
    public String hello(String name) throws InterruptedException {
        System.out.println("11111111111111111");
        Thread.sleep(100000L);
        return "Hello World!" + name;
    }
}

 

服务调用失败后自动重试

通过zuul访问http://localhost:8088/spring-cloud-provider/a/b?name=sb

控制台输出3次:11111111111111111

 

posted @ 2018-07-03 14:33  鱼我所欲也  阅读(1183)  评论(0编辑  收藏  举报