如何简单使用gateway

(1)创建 cloud-gateway-gateway9527模块

(2)引入POM依赖



org.springframework.cloud
spring-cloud-starter-gateway



org.springframework.cloud
spring-cloud-starter-netflix-eureka-client



org.springframework.boot
spring-boot-devtools
runtime
true


org.projectlombok
lombok
true


org.springframework.boot
spring-boot-starter-test
test


com.krisswen.cloud
cloud-api-commons
${project.version}


(3)编写application.yml配置文件
server:
port: 9527
spring:
application:
name: cloud-gateway

GateWay配置

cloud:
gateway:
routes:
- id: payment_routh # 路由ID , 没有固定的规则但要求唯一,建议配合服务名
uri: http://localhost:8001 # 匹配后提供服务的路由地址
predicates:
- Path=/payment/** # 断言,路径相匹配的进行路由

  - id: payment_routh2  # 路由ID , 没有固定的规则但要求唯一,建议配合服务名
    uri: http://localhost:8001  # 匹配后提供服务的路由地址
    predicates:
    - Path=/payment/lb/**  # 断言,路径相匹配的进行路由

注册进 eureka Server
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
register-with-eureka: true
fetch-registry: true
(4)编写启动类
@SpringBootApplication
@EnableEurekaClient
public class GatewayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GatewayMain9527.class,args);
}
}
(5)测试
没有使用网关前:
在这里插入图片描述

使用网关后:
在这里插入图片描述

这样子就隐藏了业务微服务端口8001,使用网关服务对微服务进行保护

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值