springboot整合gateway网关

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

2.3 搭建Gateway

本项目使用Spring Cloud Gateway作为网关下边创建网关工程。

新建一个网关工程。

工程结构

添加依赖:

XML org.springframework.cloud spring-cloud-starter-gateway com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2

配置网关的bootstrap.yaml配置文件

YAML#微服务配置 spring: application: name: gateway cloud: nacos: server-addr: 192.168.101.65:8848 discovery: namespace: ${spring.profiles.active} group: xuecheng-plus-project config: namespace: s p r i n g . p r o f i l e s . a c t i v e g r o u p : x u e c h e n g − p l u s − p r o j e c t f i l e − e x t e n s i o n : y a m l r e f r e s h − e n a b l e d : t r u e s h a r e d − c o n f i g s : − d a t a − i d : l o g g i n g − {spring.profiles.active} group: xuecheng-plus-project file-extension: yaml refresh-enabled: true shared-configs: - data-id: logging- spring.profiles.activegroup:xuechengplusprojectfileextension:yamlrefreshenabled:truesharedconfigs:dataid:logging{spring.profiles.active}.yaml group: xuecheng-plus-common refresh: true profiles: active: dev

在nacos上配置网关路由策略:

详细配置如下:

YAMLserver: port: 63010 # 网关端口 spring: cloud: gateway: # filter: # strip-prefix: # enabled: true routes: # 网关路由配置 - id: content-api # 路由id自定义只要唯一即可 # uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址 uri: lb://content-api # 路由的目标地址 lb就是负载均衡后面跟服务名称 predicates: # 路由断言也就是判断请求是否符合路由规则的条件 - Path=/content/** # 这个是按照路径匹配只要以/content/开头就符合要求 # filters: # - StripPrefix=1 - id: system-api # uri: http://127.0.0.1:8081 uri: lb://system-api predicates: - Path=/system/** # filters: # - StripPrefix=1 - id: media-api # uri: http://127.0.0.1:8081 uri: lb://media-api predicates: - Path=/media/** # filters: # - StripPrefix=1

启动网关工程通过网关工程访问微服务进行测试。

在http-client-env.json中配置网关的地址

使用httpclient测试课程查询 接口如下:

JSON### 课程查询列表 POST {{gateway_host}}/content/course/list?pageNo=2&pageSize=1 Content-Type: application/json { “auditStatus”: “202002”, “courseName”: “” }

运行观察是否可以正常访问接口 如下所示可以正常请求接口。

JSONhttp://localhost:63010/content/course/list?pageNo=2&pageSize=1 HTTP/1.1 200 OK transfer-encoding: chunked Content-Type: application/json Date: Sun, 11 Sep 2022 09:54:32 GMT { “items”: [ { “id”: 26, “companyId”: 1232141425, “companyName”: null, “name”: “spring cloud实战”, “users”: “所有人”, “tags”: null, “mt”: “1-3”, “mtName”: null, “st”: “1-3-2”, “stName”: null, “grade”: “200003”, “teachmode”: “201001”, “description”: “本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。”, “pic”: “https://cdn.educba.com/academy/wp-content/uploads/2018/08/Spring-BOOT-Interview-questions.jpg”, “createDate”: “2019-09-04 09:56:19”, “changeDate”: “2021-12-26 22:10:38”, “createPeople”: null, “changePeople”: null, “auditStatus”: “202002”, “status”: “203001”, “coursePubId”: null, “coursePubDate”: null } ], “counts”: 29, “page”: 2, “pageSize”: 1 }
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring