欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Spring cloud gateway nacos 配置

程序员文章站 2024-03-20 22:32:04
...
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>health-app-gateway</artifactId>
    <packaging>jar</packaging>

    <properties>
        <!--        <nacos.version>2021.1</nacos.version>-->
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>

    <dependencies>


        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>10.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-loadbalancer -->
        <!-- 我这里需要下面这个,没有就不能转发,而且版本号 还不能乱选,这里3.0.2可以用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>3.0.2</version>
        </dependency>




    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

pom.xml

server:
  port: 8888
  tomcat:
    accept-count: 1000


spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #表明gateway开启服务注册和发现的功能,
                        #并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,
                        #这个router将以服务名开头的请求路径转发到对应的服务
          lower-case-service-id: true #是将请求路径上的服务名配置为小写(因为服务注册的时候,
                                      #向注册中心注册时将服务名转成大写的了
    nacos:    # nacos 注册中心 是 version 2.0.2 (较新)
      config:
        server-addr: localhost:8848


  application:
    name: app-gateway




ribbon:
  read-timeout: 120000
  connect-timeout: 300000

logging:
  level:
    org:
      springframework: debug
    com:
      qdsgvision: debug


feign:
  httpclient:
    connection-timeout: 20000

application.yml

package gateway.filter;

import com.qdsgvision.common.util.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author xy
 */
@Slf4j
public class WorldFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        Pointer.lg("previous");
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            Pointer.lg(exchange,"then");
        }));
    }



    @Override
    public int getOrder() {
        return 0;
    }


}

全局过滤器

package gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;



@Slf4j
public class PartGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    @Override
    public GatewayFilter apply(Object config) {
        return ((exchange, chain) -> {//局部过滤器
            return chain.filter(exchange);
        });
    }

}

局部过滤器工厂

package gateway.config;

import com.qdsgvision.gateway.filter.AuthGatewayFilterFactory;
import com.qdsgvision.gateway.filter.AuthSignFilter;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//java配置
@Configuration
public class GatewayConfig {

    @Bean
    public AuthGatewayFilterFactory partGatewayFilterFactory(){
        return new PartGatewayFilterFactory();
    }

    @Bean
    public GlobalFilter globalFilter(){
        return new WorldFilter();
    }
}

配置类



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

//网关程序入口
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

程序入口

 

这个地址写的较全面 :https://www.cnblogs.com/crazymakercircle/p/11704077.html

这个问答解决了 503问题 :https://ask.csdn.net/questions/7413902