springcloud-gateway閘道器(動態路由以及負載均衡轉發配置)
阿新 • • 發佈:2020-08-12
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 #開啟根據服務名動態獲取路由地址