spring cloud gateway网关环境搭建

  1. 第一步,引入maven依赖,网关一般与注册中心配合使用,所以也加入了eureka客户端依赖
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

稳住,别浪,如果该Pom文件有spring boot starter web依赖,由于默认使用的tomcat,而gateway默认使用webflux,与tomcat冲突,需要排除tomcat依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  1. 添加网关启动类,如果该服务无ORM框架依赖则不需要配置exclude属性
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}
  1. 网关配置文件application.yml
server:
  port: 8003
  jetty:
    accesslog:
      enabled: true
spring:
  application:
    name: gateway-server
  profiles:
    active: dev
  servlet:
    multipart:
      max-file-size: 200MB
      enabled: true
      max-request-size: 1000MB
  main:
    allow-bean-definition-overriding: true
  cloud:
    gateway:
      routes:
        - id: WebApi
          uri: lb://web-app
          predicates:
            - Path=/web/**
          filters:
            - StripPrefix=1
  1. 开发环境eureka注册中心客户端配置application-dev.yml
eureka:
  client:
    service-url:
      defaultZone: http://root:zxy2019@192.168.2.90:8001/eureka/
  instance:
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${eureka.instance.hostname}:${server.port}
    prefer-ip-address: true

版权声明:本文为qq_20154251原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。