xmtrock
发布于 2021-06-03 / 163 阅读
0

nginx | 微服务网关gateway

本地转发。~是正则匹配来的
意思是所有的gulimall.com
都跳转到去127.0.0.1:88(网关服务)

upstream gulimall{
	server 127.0.0.1:88;
}

server{
	listen 80;
	server_name gulimall.com, *.gulimall.com, blog.xmtrock.site;
	
	#说明:如果是xmtrock.site下的/payed/,就转发到微服务,指定域名为order.gulimall.com。而由于域名是order,网关upstream就带进去微服务里自动转交到order服务中了
	location /payed/ {
		proxy_pass http://gulimall;
		proxy_set_header Host order.gulimall.com;
	}
	location / {
		proxy_pass http://gulimall; #这个gulimall是上面那个upstream的名字,不是网址
		proxy_set_header Host $host;
		proxy_set_header X-real-ip $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
	location /static/ {
		root html;
	}
}

springcloud gateway只在作为spring cloud生态系统中的网关,目标是代替netflix zuul,其不仅提供统一的路由方式,并且还基于filer链的方式提供了网关基本功能,例如:安全、监控/埋点、限流等。
pom依赖如下

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

properties大型匹配规则

spring:
  cloud:
    gateway:
      routes:
        #        - id: test_route
        #          uri: https://www.baidu.com
        #          predicates:
        #            - Query=url,baidu
        #        - id: qq_route
        #          uri: https://www.qq.com
        #          predicates:
        #            - Query=url,qq
        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**,/hello
          filters:
            - RewritePath=/api/(?<segment>.*), /$\{segment}
        - id: coupon_route
          uri: lb://gulimall-coupon
          predicates:
            - Path=/api/coupon/**
          filters:
            - RewritePath=/api/(?<segment>.*), /$\{segment}
        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            - RewritePath=/api/(?<segment>.*), /$\{segment}
        - id: ware_route
          uri: lb://gulimall-ware
          predicates:
            - Path=/api/ware/**
          filters:
            - RewritePath=/api/(?<segment>.*), /$\{segment}
        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/thirdparty/(?<segment>.*), /$\{segment}

        # 注意优先级问题,上面都路由不到才会到下面,因为上面都带服务名,下面则只有renrenfast特殊点
        - id: admin_route
          uri: lb://renren-fast #lb是low balance负载均衡
          predicates:
            - Path=/api/**
          filters:
            #/api开始的东西,都重写为/renren-fast开头的东西,这是很奇妙的东西,注意逗号隔开哟
            - RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment} #因为renren-fast服务的servlet.context-path是renre-fast

        # 以下是根据域名类匹配nginx转发过来的请求
        - id: gulimall_host_route
          uri: lb://gulimall-product #lb是low balance负载均衡
          predicates:
            - Host=gulimall.com,item.gulimall.com
        - id: gulimall_search_host_route
          uri: lb://gulimall-search #lb是low balance负载均衡
          predicates:
            - Host=search.gulimall.com
        - id: gulimall_auth_route
          uri: lb://gulimall-auth-server #lb是low balance负载均衡
          predicates:
            - Host=auth.gulimall.com
        - id: gulimall_cart_route
          uri: lb://gulimall-cart #lb是low balance负载均衡
          predicates:
            - Host=cart.gulimall.com
        - id: gulimall_order_route
          uri: lb://gulimall-order #lb是low balance负载均衡
          predicates:
            - Host=order.gulimall.com
        - id: gulimall_order_route
          uri: lb://gulimall-member #lb是low balance负载均衡
          predicates:
            - Host=member.gulimall.com
        - id: gulimall_seckill_route
          uri: lb://gulimall-seckill #lb是low balance负载均衡
          predicates:
            - Host=seckill.gulimall.com

跨域问题

@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);//带cookie

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}