SpringCloud-Finchley集成Zuul服务网关组件并实现回退

1.服务网关Zuul介绍

服务网关是微服务架构中一个不可或缺的部分。通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由、均衡负载功能之外,它还具备了权限控制等功能。Spring Cloud Netflix中的Zuul就担任了这样的一个角色,为微服务架构提供了前门保护的作用,同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,使得服务集群主体能够具备更高的可复用性和可测试性。

2.为什么要用Zuul

本图摘自方志鹏的深入理解SpringCloud微服务书籍

 

SpringCloud-Finchley集成Zuul服务网关组件并实现回退_第1张图片

3.Zuul服务网关使用的架构图示

SpringCloud-Finchley集成Zuul服务网关组件并实现回退_第2张图片

4.实现Zuul的代码:

1.pom文件



    4.0.0

    com.xueqing.demo
    service-zuul
    0.0.1-SNAPSHOT
    jar

    service-zuul
    Demo project for Spring Boot

    
        com.xueqing.demo
        springcloud1
        1.0-SNAPSHOT
    


    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.alibaba
            fastjson
            1.2.46
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.SpringBoot启动类开启Zuul注解

package com.xueqing.demo.servicezuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ServiceZuulApplication {

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

3.实现FallbackProvider接口重写两个方法

package com.xueqing.demo.servicezuul;

import com.alibaba.fastjson.JSONObject;
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;

@Component
public class MyProvinder implements FallbackProvider {

    @Override
    public String getRoute() {
        return "*";//api服务id,如果需要所有调用都支持回退,则return "*"或return null
    }

    /**
     * 如果请求用户服务失败,返回什么信息给消费者客户端
     */
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {

        return new ClientHttpResponse(){

            @Override
            public InputStream getBody() throws IOException {
                JSONObject r = new JSONObject();
                r.put("state", "9999");
                r.put("msg", "系统错误,请求失败");
                return new ByteArrayInputStream(r.toJSONString().getBytes("UTF-8"));
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                //和body中的内容编码一致,否则容易乱码
                headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
                return headers;
            }

            /**
             * 网关向api服务请求是失败了,但是消费者客户端向网关发起的请求是OK的,
             * 不应该把api的404,500等问题抛给客户端
             * 网关和api服务集群对于客户端来说是黑盒子
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {

                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {

                return HttpStatus.OK.value();
            }

            @Override
            public String getStatusText() throws IOException {

                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {


            }

        };
    }

}

 

你可能感兴趣的:(SpringCloud)