代码地址:https://gitlab.com/huangqitai/web-project
项目依赖如下:
<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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.2.5.RELEASEversion> parent> <groupId>com.hqt.webgroupId> <artifactId>web-rootartifactId> <version>1.0version> <name>web-rootname> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <packaging>pompackaging> <modules> <module>springboot-eurekaservicemodule> <module>springboot-eurekaclientmodule> <module>springboot-gatewaymodule> modules> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build> project>
1、创建模块,名为springboot-eurekaservice
2、引入注册中心依赖
<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.0modelVersion> <parent> <artifactId>web-rootartifactId> <groupId>com.hqt.webgroupId> <version>1.0version> parent> <groupId>com.hqt.eurekagroupId> <artifactId>springboot-eurekaserviceartifactId> <version>1.0version> <name>springboot-eurekaservicename> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>4.0.1version> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> <version>2.2.5.RELEASEversion> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> 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、启动服务,如下表示服务端搭建成功
1、创建模块,名为:springboot-eurekaclient
2、客户端依赖引入
<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.0modelVersion> <parent> <artifactId>web-rootartifactId> <groupId>com.hqt.webgroupId> <version>1.0version> parent> <groupId>com.hqt.eurekaclientgroupId> <artifactId>springboot-eurekaclientartifactId> <version>1.0version> <name>springboot-eurekaclientname> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> <version>2.2.5.RELEASEversion> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> 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、启动客户端,如下表示注册成功
1、创建模块,名为 springboot-gateway
2、引入依赖
<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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.2.5.RELEASEversion> parent> <groupId>com.hqt.module.gatewaygroupId> <artifactId>springboot-gatewayartifactId> <version>1.0version> <name>springboot-gatewayname> <description>Demo project for Spring Bootdescription> <properties> <java.version>1.8java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> <version>2.2.5.RELEASEversion> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-gatewayartifactId> <version>2.2.5.RELEASEversion> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> 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、启动模块
成功通过服务注册应用名,由网关访问提供的接口
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、访问示例:
添加请求头访问: