zoukankan      html  css  js  c++  java
  • SpringCloud-05-Zuul路由网关


    Zuul很简单,大致是这么玩的:

    Zuul它本身也是一个注册在Eureka的微服务
    它为其他的微服务提供一个统一的对外的窗口
    这样便隐藏了服务的地址,同时也可以隐藏服务名,并且限定如何访问服务

    依赖

    <!--实体类 + web-->
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>
        <!--ribbon-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!-- eureka  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--Hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--actuator完善监控信息-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--zuul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
    </dependencies>
    View Code

    配置

    server:
      port: 9527
    spring:
      application:
        name: springcloud-zuul
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: zuul9527.com
        prefer-ip-address: true
    info:
      app.name: gfpz-springcloud
      company.name: blog.possible2dream.cn
    zuul:
      routes:
        mydept.serviceId: springcloud-provider-dept
        mydept.path: /mydept/**
      ignored-services: "*" #不能再使用这个路径访问了  *隐藏全部的微服务
      prefix: /knight  #设置公共的前缀

    开启注解

    @SpringBootApplication
    @EnableZuulProxy
    public class ZuulApplication_9527 {
        public static void main(String[] args) {
            SpringApplication.run(ZuulApplication_9527.class, args);
        }
    }

    host配置

    127.0.0.1 www.possible2dream.cn

    注册中心可以看到Zuul已经注册

    http://eureka7001.com:7001/

    通过Zuul所在的主机对服务进行调用(加了前缀,隐藏了服务名,隐藏了服务的IP,并且不允许带服务名访问(过滤))

    http://www.possible2dream.cn:9527/knight/mydept/dept/get/2

    (mydept实际等于标识了要访问的哪个服务,起到了路由的作用)

    总结

    Zuul包含了对请求的路由和过滤两个主要功能丶

    至此,Springcloud Netflix五大神兽就已经完结:Ribbon,Eureka,Feign,Hystrix,Zuul丶


    https://github.com/ChenCurry/springcloud.git


    击石乃有火,不击元无烟!!
  • 相关阅读:
    [IDEs]Eclipse For Mac , 常用快捷键
    Songs
    [Android学习笔记]扩展application
    [Android学习笔记]Context简单理解
    Activity组件的生命周期
    [数据结构和算法]快速排序笔记
    关于项目团队管理的几点思考
    【转】一步步教你读懂NET中IL(图文详解)
    【札记】设计的五个原则
    【转】高并发情况下的单例模式
  • 原文地址:https://www.cnblogs.com/rain2020/p/13536631.html
Copyright © 2011-2022 走看看