hai-gateway 服务端
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
添加配置
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: haiproduct # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
# uri: http://localhost:8200 # 单个匹配
uri: lb://HAI-PRODUCT # 负载均衡,可以访问 8200/8201
predicates:
- Path=/product/** # 断言,根据路径路由 http://localhost:7000/product
- id: haiapp # 路由的ID,没有固定规则,但要求唯一,建议配合服务名
uri: lb://HAI-APP # 负载均衡,可以访问 8080/8081
predicates:
- Path=/** # 断言,根据路径路由 http://localhost:7000
启动类加上注解
@EnableEurekaClient
@SpringBootApplication
**hai-app 客户端注册到 Eureka **
可以通过 gateway 访问 hai-app:
http://localhost:7000