Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee 朱秋贵内科诊所 My腾云code

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
###注册 中心
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
server:  ##api网关端口号
  port: 80
###网关名称
spring:   ##网关服务名称
  application:
    name: service-zuul
<strong>### 配置网关反向代理   
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   ##别名
</strong>

  

添加到依赖:

1
2
3
4
       <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 可以实现手动刷新 

 

yml中添加:

1
2
3
4
5
6
7
8
###默认服务读取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

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
###注册 中心
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读取的,成功!

 

posted @ 2020-07-14 15:06  cn2024  阅读(471)  评论(0编辑  收藏  举报