springboot+eureka+gateway实现网关模型
springboot+eureka+gateway实现网关模型
模型代码地址:https://gitlab.com/huangqitai/web-project
1、开发工具选择idea,创建一个项目,我名为web-root
项目依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<groupId>com.hqt.web</groupId>
<artifactId>web-root</artifactId>
<version>1.0</version>
<name>web-root</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>pom</packaging>
<modules>
<module>springboot-eurekaservice</module>
<module>springboot-eurekaclient</module>
<module>springboot-gateway</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、注册中心服务端搭建
1、创建模块,名为springboot-eurekaservice
2、引入注册中心依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>web-root</artifactId> <groupId>com.hqt.web</groupId> <version>1.0</version> </parent> <groupId>com.hqt.eureka</groupId> <artifactId>springboot-eurekaservice</artifactId> <version>1.0</version> <name>springboot-eurekaservice</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--引入新的--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
eureka服务端依赖:spring-cloud-starter-netflix-eureka-server
3、配置文件 application.yml
server: port: 8083 servlet: context-path: / # spring 相关配置 spring: main: allow-bean-definition-overriding: true application: name: springboot-eureka-service servlet: multipart: max-file-size: 100MB max-request-size: 100MB eureka: client: #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false #表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false,本身就是服务端,不向自己注册自己 register-with-eureka: false service-url: #注册中心地址 defaultZone: http://localhost:8083/ instance: appname: springboot-eureka-service server: enable-self-preservation: false eviction-interval-timer-in-ms: 10000 # 模块配置 gisquest-service: logging: level: com.gisquest: info com.springboot.test.mapper: debug file: log/MineEureka.log
4、启动服务,如下表示服务端搭建成功
3、服务提供者,eureka客户端搭建
1、创建模块,名为:springboot-eurekaclient
2、客户端依赖引入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>web-root</artifactId> <groupId>com.hqt.web</groupId> <version>1.0</version> </parent> <groupId>com.hqt.eurekaclient</groupId> <artifactId>springboot-eurekaclient</artifactId> <version>1.0</version> <name>springboot-eurekaclient</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
客户端依赖:spring-cloud-starter-netflix-eureka-client
3、配置文件 application.yml
server: port: 8084 servlet: context-path: / #spring 相关配置 spring: main: allow-bean-definition-overriding: true servlet: multipart: max-file-size: 100MB max-request-size: 100MB application: name: springboot-eureka-client eureka: instance: appname: springboot-eureka-client prefer-ip-address: true client: register-with-eureka: true enabled: true fetch-registry: true service-url: defaultZone: http://localhost:8083/eureka/ #模块配置 gisquest-client: logging: level: com.gisquest: info com.springboot.test.mapper: debug file: log/MineEurekaClient.log
4、编写一个接口作为示例
5、启动客户端,如下表示注册成功
4、gateway搭建
1、创建模块,名为 springboot-gateway
2、引入依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <groupId>com.hqt.module.gateway</groupId> <artifactId>springboot-gateway</artifactId> <version>1.0</version> <name>springboot-gateway</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.5.RELEASE</version> </dependency> <!--在springboot的pom文件中,该依赖已经集成了springMVC等web启动器,不需要再添加spring-boot-starter-web 依赖了--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>2.2.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
网关依赖:spring-cloud-starter-gateway
3、配置文件application.yml
server: port: 8082 servlet: context-path: / # spring 相关配置 spring: main: allow-bean-definition-overriding: true application: name: springboot-gateway cloud: gateway: discovery: locator: enabled: true #开启Eureka服务发现 lower-case-service-id: true eureka: client: service-url: defaultZone: http://localhost:8083/eureka/ #Eureka Server地址 instance: appname: springboot-gateway prefer-ip-address: true # 模块配置 gisquest-gateway: logging: level: com.gisquest: info com.springboot.test.mapper: debug file: log/MineGateway.log
4、启动模块
5、测试接口,通过网关访问服务提供接口
成功通过服务注册应用名,由网关访问提供的接口
6、添加网关拦截校验(模拟登录校验)
1、创建拦截器
package com.hqt.module.gateway.config; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.HashMap; import java.util.Map; @Component public class LoginFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String accessToken = request.getHeaders().getFirst("token"); if ("xxx".equals(accessToken)){ chain.filter(exchange); return chain.filter(exchange); }else { return loginResponse(exchange); } } @Override public int getOrder() { return 0; } public static Mono<Void> loginResponse(ServerWebExchange exchange) { Map<String,Object> resultJson = new HashMap<>(); resultJson.put("code", 401); resultJson.put("message", "请重新登陆授权"); resultJson.put("status", 401); ServerHttpResponse response = exchange.getResponse(); byte[] bytes = resultJson.toString().getBytes(); response.getHeaders().add("Content-Type", MediaType.APPLICATION_JSON_VALUE); DataBuffer buffer = response.bufferFactory().wrap(bytes); return response.writeWith(Flux.just(buffer)); } }
2、访问示例:
添加请求头访问:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南