• SpringCloud Netflix-Zuul使用


    1、介绍

    项目github地址:https://github.com/Netflix/zuul

    zuul是netflix开源的一个API网关服务,本质上也是一个HTTP服务器。zuul已经整合到SpringCloud生态中,作为SpringCloudNetflix的一个组件,在微服务架构中从当API网关角色。

    在这里插入图片描述

    由上图可以看出,在微服务架构中,zuul作为整个系统访问的统一入口,具备了身份认证鉴权、动态路由、负载均衡、服务容错等一系列功能,在整个系统中至关重要。

    在这里插入图片描述

    2、使用

    详细演示代码:https://gitee.com/micro_service_xk/netflix-cloud-demo.git zuul模块

    1、导入zuul依赖

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-zuulartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    2、开启zuul网关功能

    @EnableDiscoveryClient //开启Eureka客户端
    @EnableZuulProxy // 开启Zuul的网关功能
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    application.yml配置文件

    server:
      port: 10010 # 服务端口
    spring:
      application:
        name: zuul-service # 服务名,将注册到eureka-server中
    eureka:
      client:
        service-url:  # eureka-server的地址信息,多个使用英文逗号分割
          defaultZone: http://127.0.0.1:10086/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3、演示zuul路由

    注册到eureka服务端的服务实例列表:

    在这里插入图片描述

    携带服务名访问zuul,zuul将动态路由到指定服务。例如访问http://localhost:10010/order-service/order/1,且zuul已集成robbin、hystix,已实现多实例路由负载均衡及熔断。

    在这里插入图片描述

    其它配置信息:

    zuul:
      retryable: true #zuul开启重试
      prefix: /api #zuul网关访问前缀
    
    ribbon:
      ConnectTimeout: 250 # Ribbon的连接超时时间(创建会话的连接时间,不是服务的响应时间,而是本机与服务建立会话的时间。)
      ReadTimeout: 1000 # Ribbon的数据读取超时时间(当请求发出之后,如果对方服务没有在规定时间内返回)
      OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
      MaxAutoRetriesNextServer: 1 # 切换实例的重试次数(默认1)
      MaxAutoRetries: 0 # 对当前实例的重试次数(默认0)
    
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 3000 # 设置hystrix的降级超时时间为3000ms,对所有远程服务调用生效
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    使用重试机制需要引入spring-retry依赖

    <dependency>
        <groupId>org.springframework.retrygroupId>
        <artifactId>spring-retryartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    使用zuul降级操作,需要编写降级类(示例:)

    @Component
    public class OrderServiceFallBack implements FallbackProvider {
        @Override
        public String getRoute() {
            /**
             * 指定对哪个服务进行降级操作,* 代表对所有服务进行降级
             */
            return "order-service";
        }
    
        @Override
        public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
            return new ClientHttpResponse() {
                @Override
                public HttpStatus getStatusCode() throws IOException {
                    return HttpStatus.BAD_GATEWAY;
                }
    
                @Override
                public int getRawStatusCode() throws IOException {
                    return HttpStatus.BAD_REQUEST.value();
                }
    
                @Override
                public String getStatusText() throws IOException {
                    return HttpStatus.BAD_REQUEST.getReasonPhrase();
                }
    
                @Override
                public void close() {
    
                }
    
                @Override
                public InputStream getBody() throws IOException {
                    String msg = "{\"code\":-1,\"msg\":\"远程服务调用失败\"}";
                    return new ByteArrayInputStream(msg.getBytes());
                }
    
                @Override
                public HttpHeaders getHeaders() {
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_JSON);
                    return headers;
                }
            };
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4、演示zuul过滤器

  • 相关阅读:
    商城接口文档
    Revive开发商加入VR开源标准OpenXR
    最新python大数据毕业设计选题推荐(三)
    您的计算机已被Mallox勒索病毒感染?恢复您的数据的方法在这里!
    linuxPTP参数详解
    车载网络扫盲
    模仿 mapstruct 实现一个微服务编排框架(上)
    BS-GX-017基于SSM实现的在线考试管理系统
    模拟SQLserver死锁现象
    工序解释执行程序--工程师的成长
  • 原文地址:https://blog.csdn.net/xiao_yu_gan/article/details/126605585