使用spring cloud gateway作为服务网关

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。

gateway需要注册到nacos中去,需要引入以下的依赖:

<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-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
        

在配置文件application.pom文件:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: nacos-provider
          uri: http://localhost:8762
          predicates:
            - Path=/nacos-provider/**
          filters:
            - StripPrefix=1
        - id: order-domain
          # uri: lb://order-domain
          uri: http://localhost:8763
          predicates:
            - Path=/order-domain/**
          filters:
            - StripPrefix=1

在工程的启动文件加上相关注解

@SpringBootApplication
@EnableDiscoveryClient
public class gatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(gatewayApplication.class,args);
    }
}

成功

你可能感兴趣的:(Spring,spring,cloud,后端)