配置服务的路由:编码方式

1、删除配置文件 application.yml

2、修改 Application.java, 添加自定义路由配置

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;

import org.springframework.cloud.gateway.route.RouteLocator;

import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

public class Application {

@Bean

public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();

config.setParts(1);

return builder.routes()

.route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://www.198bona.com"))

.route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://www.198bona.com"))

.build();

}

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

(0)

相关推荐