ITPub博客

首页 > IT职业 > IT职场 > 使用Spring Cloud Gateway 替换 zuul, 并代理

使用Spring Cloud Gateway 替换 zuul, 并代理

IT职场 作者:qyqy0139 时间:2021-09-09 10:14:14 0 删除 编辑

阅读本篇,需要你提前了解并做好:

  • spring boot 和 spring cloud 相信因为标题进来的你已经了解并应用了

  • 下载安装并启动 Nacos, nacos 快速入门

  • 将服务注册中心替换为 Nacos, 可以参考 Nacos与Spring Cloud快速入门

  • spring boot 使用 2.0.x, 不要使用 2.1.x, 因为 Nacos 的相关库还没有适配 2.1.x ,会有问题(比如服务注册不上)

  • spring cloud 使用 Finchley.SR1 或者  Finchley.SR2

pom 引入:

~~~        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency> 
        <!-- nacos 的服务注册与发现 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- hystrix 熔断器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>~~~

application.yml

server:  port: 80     
spring:  application:    name: gateway-server
  cloud: 
    nacos: 
      discovery:        serverAddr: 127.0.0.1:8848

启动器和简单路由+Hystrix熔断降级

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.gateway.route.RouteLocator;import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import reactor.core.publisher.Mono;@SpringBootApplication@EnableDiscoveryClient  // 启用服务注册和发现@RestController  // 提供一个简单的降级页面public class GatewayServerApplication {    public static void main(String[] args) {
    SpringApplication.run(GatewayServerApplication.class);
    }    
  /**
     * @Title: fallback
     * @Description: 一个简单的降级页面
     * @return
     */
    @RequestMapping("/fallback")    public Mono<String> fallback() {        // Mono是一个Reactive stream,对外输出一个“fallback”字符串。
        return Mono.just("fallback");
    }    
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {    return builder.routes()                // 配制一个路由,把 http://网关地址:网关端口/demo/ 下的请求路由到 demo-service 微服务中
        .route(p -> p
                    .path("/demo/**") 
                    .filters(f -> f
                        .hystrix(config -> config   // 对path()指定的请求使用熔断器
                                    .setName("mycmd")   // 熔断器的名字
                                    .setFallbackUri("forward:/fallback")))  // 熔断到 /fallback, 就是上面配制的那个 
                    .uri("lb://demo-service"))  // 将请求路由到指定目标, lb开头是注册中心中的服务, http/https 开头你懂的
        .build();
    }

}



作者:邪影oO
链接:https://www.jianshu.com/p/0bae4ff0d809


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1350/viewspace-2818666/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    84
  • 访问量
    970