美文网首页
微服务 之 统一网关

微服务 之 统一网关

作者: 诺之林 | 来源:发表于2020-10-05 13:19 被阅读0次

    本文的示例代码参考gateway-app / gateway-zuul / gateway-springcloud

    简介

    • 功能
    路由
    
    过滤 => 权限、监控、限流、日志
    
    • 价值
    复用 => 限流、日志
    
    安全 => 权限、监控
    

    应用

    spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web --build=gradle gateway-app && cd gateway-app
    
    vim src/main/kotlin/com/example/gatewayapp/BooksController.kt
    
    package com.example.gatewayapp
    
    import org.springframework.web.bind.annotation.GetMapping
    import org.springframework.web.bind.annotation.PathVariable
    import org.springframework.web.bind.annotation.RequestHeader
    import org.springframework.web.bind.annotation.RestController
    
    @RestController
    class BooksController {
        @GetMapping("/{id}")
        fun findById(@PathVariable id: Long,
                     @RequestHeader(name = "Proxy", required = false) proxy: String?): String {
            return  (proxy ?: "") + "|books => " + id;
        }
    }
    
    vim ./src/main/resources/application.properties
    
    server.port=5001
    
    ./gradlew bootrun
    
    curl localhost:8090/2
    # |books => 2
    
    curl localhost:5001/2 -H "Proxy: local"
    # local|books => 2
    

    Netflix Zuul

    spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=web,cloud-zuul --build=gradle gateway-zuul && cd gateway-zuul
    
    vim src/main/kotlin/com/example/gatewayzuul/DemoApplication.kt
    
    package com.example.gatewayzuul
    
    import org.springframework.boot.autoconfigure.SpringBootApplication
    import org.springframework.boot.runApplication
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy
    
    @EnableZuulProxy
    @SpringBootApplication
    class DemoApplication
    
    fun main(args: Array<String>) {
        runApplication<DemoApplication>(*args)
    }
    
    vim src/main/resources/application.properties
    
    server.port=6001
    spring.application.name=gateway-zuul
    zuul.routes.books.path: /books/**
    zuul.routes.books.url: http://localhost:5001/
    
    vim src/main/kotlin/com/example/gatewayzuul/SimpleFilter.kt
    
    package com.example.gatewayzuul;
    
    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.context.RequestContext;
    import com.netflix.zuul.exception.ZuulException;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SimpleFilter extends ZuulFilter {
    
        @Override
        public String filterType() {
            return "pre";
        }
    
        @Override
        public int filterOrder() {
            return 0;
        }
    
        @Override
        public boolean shouldFilter() {
            return true;
        }
    
        @Override
        public Object run() throws ZuulException {
            RequestContext ctx = RequestContext.getCurrentContext();
            ctx.addZuulRequestHeader("Proxy", "SimpleFilter");
            return null;
        }
    }
    
    ./gradlew bootrun
    
    curl localhost:5001/2
    # |books => 2
    
    curl localhost:6001/books/2
    # gateway-zuul|books => 2
    

    Spring Cloud Gateway

    spring init -b=2.2.10.RELEASE -j=1.8 -l=kotlin -d=cloud-gateway --build=gradle gateway-springcloud && cd gateway-springcloud
    
    rm src/main/resources/application.properties
    
    vim src/main/resources/application.yml
    
    server:
      port: 7001
    spring:
      application:
        name: gateway-springcloud
      cloud:
        gateway:
          routes:
            - id: books_route
              uri: http://localhost:5001/
              predicates:
                - Path=/books/**
              filters:
                - StripPrefix=1
    
    ./gradlew bootrun
    
    curl localhost:5001/2
    # |books => 2
    
    curl localhost:7001/books/2 -H "Proxy: gateway-springcloud"
    # gateway-springcloud|books => 2
    

    参考

    相关文章

      网友评论

          本文标题:微服务 之 统一网关

          本文链接:https://www.haomeiwen.com/subject/iwqnuktx.html