SpringCloud 学习笔记系列 06----Zuul 整合Config实现动态路由

Zuul 整合Config实现动态路由

Zuul配置
  • pom.xml
    •   <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-config</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
                </dependency>
                <!--健康监控,用于配置文件refresh-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-actuator</artifactId>
                </dependency>
      
  • application.yml
    •   spring:
          application:
            name: hyq-zuul-server
          profiles:
            active: test
        #端口
        server.port: 4002
        # setting up spring eureka service discovery
        eureka:
          instance:
            prefer-ip-address: true
            instance-id: ${spring.application.name}:${server.port}
      
          client:
            serviceUrl:
        #      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/
              defaultZone: http://admin:admin@localhost:8081/eureka/
      
  • bootstrap.yml
    •   # config 配置中心配置-->只能放在bootstrap.yml文件中才能生效
        spring:
          cloud:
              config:
                uri: http://localhost:5001
                profile: test
                label: master
                username: admin
                password: admin
      
  • 码云配置文件:https://gitee.com/hyman.hu/springcloud-config-server/blob/master/config/application-test.yml
    • application-test.yml
      •   # 路由配置:实际开发过程中放到config配置中心
          zuul:
            routes:
               # 别名
              eureka-client:
                # 通配符说明:
                #   ?: 匹配任意单字符 eg:/client/? 匹配 /client/a  /client/b
                #   *: 匹配任意数量字符 eg: /client/* 匹配 /client/user
                #  **: 匹配任意数量字符,支持多久目录 eg: /client/** 匹配 /client/user/info
                path: /client/**
                serviceId: HYQ-EUREKA-CLIENT  #如果是/ribbon/**路径下的请求,则跳转到service-ribbon
              eureka-server:
                path: /server/**
                serviceId: HYQ-HSYTRIX-CLIENT  #如果是/feign/**路径下的请求,则跳转到service-feign
              cinfig-client:
                path: /config-client/**
                serviceId: HYQ-CONFIG-CLIENT  #如果是/feign/**路径下的请求,则跳转到service-feign
            # 使用zuul.ignored-services配置需要忽略的服务,多个用逗号分隔
            ignored-services: '*' #只代理配置的微服务
            #ignored-patterns: /client/** # 忽略所有包含client/** 的路径
          # 暴露 routes 接口:默认暴露health,info
          # 查看以及管理Zuul的路由:http://localhost:4001/actuator/routes
          management:
            endpoints:
                web:
                  exposure:
                    include: routes,refresh
        
        
          # 配默认熔断超时策略
          # zuul配置了熔断fallback的话,熔断超时也要配置,
          # 不然如果你配置的ribbon超时时间大于熔断的超时,那么会先走熔断,相当于你配的ribbon超时就不生效了。
          hystrix:
            command:
              default:
                execution:
                  isolation:
                    thread:
                      timeoutInMilliseconds: 60000
        
          # ribbon超时时间设置
          ribbon:
            ReadTimeout: 60000
            ConnectTimeout: 60000
        
  • 启动类:ZuulConfigApplication.java
    •   @SpringBootApplication
        @EnableZuulProxy
        @EnableEurekaClient
        public class ZuulConfigApplication {
      
            public static void main(String[] args) {
                SpringApplication.run(ZuulConfigApplication.class, args);
            }
      
            @Bean
            @RefreshScope
            @ConfigurationProperties("zuul")
            @Primary //必须
            public ZuulProperties zuulProperties(){
                return new ZuulProperties();
            }
        }
      
      
ConfigServer 配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值