spring cloud 网关路由信息动态配置

一、远程git仓库

api-gateway-dynamic.properties

zuul.routes.api-a.path=/feign/**
zuul.routes.api-a.serviceId=eureka-feign-consumer
远程仓库配置了一条网关路由,网关通过配置服务器获取该信息


二、网关api-gateway-dynamic

pom.xml

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
  </parent>
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zuul</artifactId>
      <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
      <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>1.3.1.RELEASE</version>
    </dependency>

配置信息bootstrap.properties

#网关通过配置服务端获取远程库的路由配置信息
#应用名,这里与配置仓库名一样,spring.cloud.config.name默认为应用名
spring.application.name=api-gateway-dynamic
server.port=5556

#这里用服务名连接配置服务端出现jar包版本问题,不行
#spring.cloud.config.discovery.enabled=true
#spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.uri=http://127.0.0.1:1201/

#关闭端点权限控制
management.security.enabled=false

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1001/eureka/

应用主类:

package org.api.gateway.dynamic;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;

@EnableZuulProxy
@SpringCloudApplication
public class DynamicApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(DynamicApplication.class).web(true).run(args);
    }
    
    @Bean
    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties(){
        return new ZuulProperties();
    }
}


这样,先开启服务治理中心eureka-server、配置服务端config-server,在启动应用主类,网关会获取到远程仓库配置的路由信息。

通过/routes端点可以看到网关的所有路由信息,包括默认路由信息。


修改远程参数路由配置信息,在发送post请求/refresh到网关刷新配置就可以了。


2、从数据库中读取路由信息,并动态刷新

https://github.com/lexburner/zuul-gateway-demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值