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

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   ##别名
### 配置网关反向代理    
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刷新下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值