给微服务项目引入GateWay处理跨域和统一路由

334 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天,点击查看活动详情

GateWay配置

GateWay回顾

核心的逻辑:路由转发 + 执行过滤器链。

image-20220731114052109

跨域问题

跨域:指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。

例如:a页面想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源。注意:跨域限制访问,其实是浏览器的限制。理解这一点很重要!!!

同源策略:是指协议,域名,端口都要相同,其中有一个不同都会产生跨域

如何解决跨域问题?

由之前的介绍我们已经知道错误的原因,既然跨域会产生问题,那么我们就不跨域不就完了嘛!!!

1.注解@CrossOrigin,在后端接口加上@CrossOrigin即可解决跨域问题

2.使用网关解决,配置跨域配置类进行设置

前端配置统一的请求地址

window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api';

后端的网关地址是多少就填多少,这样所有的请求都会发到http://网关地址:88/api这个uri

image-20220731110417031

将微服务接入nacos

这里通过引入mall-common模块让其他模块启程它的依赖

这里renren-fast版本要改成和你Springboot版本一样,如下:

  1. springboot版本和sprincloud有版本对应,mall-common模块引入的cloud版本都是对应好的
  2. 包的冲突

因为renren-fast本来用的是2.6.6,它和低版本还有个点不同就是配置跨域的配置文件时2.6.6用的是.allowedOriginPatterns

而2.4版本以下的用的是.allowedOrigins,所以进行替换即可将 .allowedOriginPatterns替换为.allowedOrigins

如果还有冲突,就用maven helper排查即可

https://blog.csdn.net/kingwinstar/article/details/106916140?spm=1001.2014.3001.5506

image-20220731111240299

那么将微服务引入nacos注册中心只要三步

  1. 引入依赖(mall-common)
  2. 添加注解(启动类添加@EnableDiscoveryClient)
  3. 配置文件(指定nacos注册中心地址和微服务名称)

renren-fast的dev配置文件加上如下内容

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: renren-fast

网关的properties文件加上如下内容

# 应用名称
spring.application.name=mall-gateway
server.port=88
#服务发现
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

注册成功如下

image-20220731171048839

设置路由

Spring Cloud Gateway 路径重写正则表达式的理解:blog.csdn.net/weixin_4319…

这里我在gateway中配置了将nacos作为配置中心

所以通过写bootstrap.properties文件来指定地址和命名空间即可

spring.application.name=mall-gateway
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=f1212912-c9f6-4323-b9f5-1a7d8d92c5b4

配置文件如下

spring:
    gateway:
      routes:
        - id: product_route
          uri: lb://mall-product
          predicates:
            - Path=/api/product/**
          filters:
          # 将 https://网关ip/api/product/** 这个路径重写为 https://mall-product微服务地址:端口/**
            - RewritePath=/api/(?<segment>.*),/${segment}
            
      
        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
          # 将 https://网关ip/api/** 这个路径重写为 https://renren-fast微服务地址:端口/renren-fast**
            - RewritePath=/api/(?<segment>.*),/renren-fast/${segment}

配置跨域

@Configuration
public class CrosConfig {
    @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);
    }
}

renren-fast模块删除自带的跨域配置

image-20220731174131099

测试

这里配置的还有逻辑删除,在实体类上加

@TableLogic(value = "1",delval = "0")
private Integer showStatus;
​
//配置文件补充
mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0         

image-20220731200200408