一 代码位置
https://github.com/cakin24/spring-cloud-code/tree/master/ch17-3/ch17-3-2-gateway
https://github.com/cakin24/spring-cloud-code/tree/master/ch17-3/ch17-3-service
二 关键代码
package cn.springcloud.book.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SCGatewayApplication {
@Bean
public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
// 关键是下面几行代码
return builder.routes()
.route("add_request_parameter_route", r ->
r.path("/addRequestParameter").filters(f -> f.addRequestParameter("example", "ValueB"))
.uri("http://localhost:8071/test/addRequestParameter"))
.build();
}
public static void main(String[] args) {
SpringApplication.run(SCGatewayApplication.class, args);
}
}
三 测试
1 启动ch17-3-2-gateway
2 启动ch17-3-service
3 浏览器输入 http://localhost:8080/addRequestParameter
4 页面显示
return addRequestParameter info:ValueB