SpringCloud GateWay遇到的问题

SpringCloud nacos结合gateWay出现异常解决思路

我们为了解决跨域问题
首先我们配置好了Vue前端页面,让所有的请求都通过网关进行访问
统一访问地址
所有的应用都通过gateway去访问。

gateway的配置和微服务的配置

1将gateWay配置到nacos中去。

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimail-gateway
server.port=88

2.配置微服务到Nacos中去

2.1. mail-product

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://47.96.156.168:3310/gulimail_pms
    driver-class-name: com.mysql.cj.jdbc.Driver
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  #Nacos的地址

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto      #使数据库自增
server:
  port: 9130

bootstrap中配置

spring.application.name=mail-product     #nacos中显示的名字
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=051a5aa4-dd5b-4513-8639-52d4f76d38e8

2.2 renren-fast
配置相同:

配置好之后nacos中就会显示:
Nacos配置中心的结果
详细信息,包括端口号等

配置完之后我们就可以通过Nacos和gateway去进行微服务之间的访问和跨域。

1.首先我们有一个前端页面
访问的实际路径

由于最初时我们的配置我们首先会访问到网关。

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://mail-product   #要去的地方,这里一定要和nacos中的一样,即自己再product微服务中设置的application name一样没否则访问会出现403错误
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/${segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/${segment}

接下来为了跨域我们配置了一个GulimailCorsConfiguration,解决跨域问题。

@Configuration
public class GulimailCorsConfiguration  implements WebMvcConfigurer {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        /**
         * p配置跨域
         */
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }

遇到了503问题

当时解决的时候花了大概有两天的时间,就是不知道怎么解决,当时快要崩溃了。最后发现这两个地方配置一定不能有问题。
1.product的application配置文件中命名

spring.application.name=mail-product
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=051a5aa4-dd5b-4513-8639-52d4f76d38e8

2.gateway去访问也是根据这个名字去的
就是上面的名字
3.这是gateway的配置文件,里面的uri就是要访问的微服务,一定要和上面的一样。

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://mail-product   #要去的地方
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/${segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/${segment}



  ## 前端项目,/api
## http://localhost:88/api/captcha.jpg   http://localhost:8080/renren-fast/captcha.jpg
## http://localhost:88/api/product/category/list/tree http://localhost:10000/product/category/list/tree

只要这三个地方和跨域解决了,就没问题了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值