spring.application.name=gateway-java-api
server.port=50010
#id:自定义路由ID
spring.cloud.gateway.routes[0].id=weight_route1
#uri:目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello
spring.cloud.gateway.routes[0].predicates[1]=Weight=Weight,4
spring.cloud.gateway.routes[0].predicates[2]=Query=name
#id:自定义路由ID
spring.cloud.gateway.routes[1].id=weight_route2
#uri:目标服务地址
spring.cloud.gateway.routes[1].uri=http://localhost:50021
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[1].predicates[0]=Path=/hello
spring.cloud.gateway.routes[1].predicates[1]=Weight=Weight,6
#id:自定义路由ID
spring.cloud.gateway.routes[2].id=path_route1
#uri:目标服务地址
spring.cloud.gateway.routes[2].uri=http://localhost:50022
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[2].predicates[0]=Path=/test
#id:自定义路由ID
spring.cloud.gateway.routes[3].id=path_route2
#uri:目标服务地址
spring.cloud.gateway.routes[3].uri=http://localhost:50023
#predicates:路由条件。Predicate根据输入参数返回一个布尔值。其包含多种默认方法来将Predicate组合成复杂的路由逻辑
spring.cloud.gateway.routes[3].predicates[0]=Path=/test
@RestController
public class HelloController {
@Value("${provider.name}")
private String name;
@GetMapping("/hello")
public String hello(@RequestParam(value = "name",required = false) String name){
if (StringUtils.isEmpty(name)){
name=this.name;
}
return name;
}
@GetMapping("/hello2")
public String hello2(){
return name;
}
@GetMapping("/test")
public String test(){
return name;
}
}
端口号分别是50020,50021,50022,50023
名称分别是provider-1,provider-2,provider-3,provider-4
1、访问http://localhost:50010/hello?name=liu
2、访问http://localhost:50010/hello
3、访问http://localhost:50010/test
总结: