springcloud-gateway网关(动态路由以及负载均衡转发配置)
1.依赖
<!--引入gateway网关依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--consul 依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--监控检查依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.启动注解
@SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
3.配置文件
server: port: 8888 spring: application: name: gateway cloud: consul: port: 8500 host: localhost discovery: service-name: ${spring.application.name} gateway: routes: - id: user_route uri: lb://userservices # lb: 使用负载均衡策略 userservices代表注册中心的具体服务名 predicates: - Path=/user/** - id: product_route uri: lb://productservices # lb: 使用负载均衡策略 productservices代表注册中心的具体服务名 predicates: - Path=/product/** discovery: locator: enabled: true #开启根据服务名动态获取路由地址