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>
<!--监控检查依赖-->springcloud和springboot
<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 #开启根据服务名动态获取路由地址
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论