-
yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启注册中心路由功能
# lower-case-service-id: true
routes:
- id: payment_routh
# uri: http://localhost:8001 #单机
uri: lb://cloud-payment-service #此处如果有问题,请注意依赖spring-cloud-starter-netflix-eureka-client依赖不能错
predicates:
- Path=/payment/get/**
- id: payment_routh2
# uri: http://localhost:8001
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka #,http://eureka7002.com:7002/eureka
#logging:
# level:
# root: debug
2.代码中注入RouteLocator的Bean
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route"
, r->r.path("/guonei").uri("http://news.baidu.com/guonei"))
.build();
routes.route("path_route2",r->r.path("/guoji").uri("http://news.baidu.com/guonei")).build();
return routes.build();
}
}