• Spring Cloud之搭建动态Zuul网关路由转发


    传统方式将路由规则配置在配置文件中,如果路由规则发生了改变,需要重启服务器。这时候我们结合上节课内容整合SpringCloud Config分布式配置中心,实现动态路由规则。

    将yml的内容粘贴到码云上:

    ###注册 中心
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8100/eureka/
    server:  ##api网关端口号
      port: 80
    ###网关名称
    spring:   ##网关服务名称
      application:
        name: service-zuul
    ### 配置网关反向代理    
    zuul:
      routes:
        api-member:  ##随便写的
         ### 以 /api-member/访问转发到会员服务   通过别名找
          path: /api-member/**
          serviceId: app-toov5-member  ##别名  如果集群的话  默认整合了ribbon 实现轮训 负载均衡
        api-order:   ##随便写的
            ### 以 /api-order/访问转发到订单服务
          path: /api-order/**
          serviceId: app-toov5-order   ##别名
    

      

    添加到依赖:

                   <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-actuator</artifactId>
    		</dependency>
    

     可以实现手动刷新 

    yml中添加:

    ###默认服务读取eureka注册服务列表 默认间隔30秒
    
    ###开启所有监控中心接口
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    

      开启所有监控中心接口

    启动类里面添加:

     //zuul配置使用config实现实时更新 
       @RefreshScope
       @ConfigurationProperties("zuul")
       public ZuulProperties zuulProperties() {
           return new ZuulProperties();
       }

    package com.toov5;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableZuulProxy  //开启网关代理
    public class AppGateway {
       public static void main(String[] args) {
       SpringApplication.run(AppGateway.class, args);     
    }
       //zuul配置使用config实现实时更新 
       @RefreshScope
       @ConfigurationProperties("zuul")
       public ZuulProperties zuulProperties() {
           return new ZuulProperties();
       }
       
       
    }

    yml

    ###注册 中心
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8100/eureka/
    server:  ##api网关端口号
      port: 80
    ###网关名称
    spring:   ##网关服务名称
      application:
        name: service-zuul
      ###网关名称  
      cloud:
        config:
        ####读取后缀
          profile: dev
          ####读取config-server注册地址
          discovery:
            service-id: confi
        
    ### 配置网关反向代理    
    #zuul:
    #  routes:
    #    api-member:  ##随便写的
    #     ### 以 /api-member/访问转发到会员服务   通过别名找
    #      path: /api-member/**
    #      serviceId: app-toov5-member  ##别名  如果集群的话  默认整合了ribbon 实现轮训 负载均衡
    #    api-order:   ##随便写的
    #        ### 以 /api-order/访问转发到订单服务
    #      path: /api-order/**
    #      serviceId: app-toov5-order   ##别名
    

      

     启动eureka和configserver

    访问: 

     可以读取到

    启动 gateway

    然后启动 member 

     访问:

     

    配置文件是从git读取的,成功!

     发生变更后同样需要 post刷新下。

  • 相关阅读:
    高效 告别996,开启java高效编程之门 3-16收集器与预定义收集器概述
    高效 告别996,开启java高效编程之门 3-15实战:流的构建四种形式
    高效 告别996,开启java高效编程之门 3-14常用操作总结与流构建描述
    高效 告别996,开启java高效编程之门 3-13实战:常用终端操作之最大/最小/计数
    高效 告别996,开启java高效编程之门 3-12实战:常用终端操作之查找
    高效 告别996,开启java高效编程之门 3-11实战:常用终端操作之匹配
    cube中的判断类型
    正则表达式解析基本json
    敏捷开发入门教程-----摘抄
    单页面路由样例
  • 原文地址:https://www.cnblogs.com/toov5/p/9972732.html
Copyright © 2020-2023  润新知