从头搭建spring cloud + spring cloud alibaba踩坑日志<二>

106 阅读1分钟

融入gateway

在gateway项目中加入了spring-boot-starter-web依赖导致报错

Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.
在引入gateway时出现路由失效的情况;

    gateway:
      routes:
         #路由ID,没有固定要求,但是要保证唯一,建议配合服务名
        - id: auth
          #uri: http://127.0.0.1:9080 认证中心的服务地址
          uri: lb://auth
          order: 8000
          predicates:
            - Path=/auth/**

使用http://127.0.0.1:9080地址路由时能正常路由,但是使用lb://auth无法路由 经查证是因为没有加入依赖导致

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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
      <artifactId>family-record</artifactId>
        <groupId>com.wuwanran</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>com.wuwanran</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>gateway</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- LB 扩展 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
    </dependencies>



</project>

bootstrap.yml

# Tomcat
server:
  port: 9090

# Spring
spring: 
  application:
    # 应用名称
    name: gateway
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848

    gateway:
      routes:
        - id: auth
#          uri: http://127.0.0.1:9080 认证中心的服务地址
          uri: lb://auth
          order: 8000
          predicates:
            - Path=/auth/**

向前辈学习