1、创建一个maven项目
next
Next
Finish完成.
2、在创建好的maven项目上右键New->Module
选择Spring initializr创建eureka注册中心
Next
Next
Next
Finish.
打开eureka-service项目下的application.properties配置文件,将.properties重命名为.yml,配置如下
server: port: 8001 #端口号 spring: application: name: eureka-service #服务名称 eureka: instance: hostname: localhost client: fetch-registry: false #是否从Eureka Server获取注册信息 register-with-eureka: false #是否将自己注册到Eureka Server service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服务地址
然后启动类中增加@EnableEurekaServer注解就可以了
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
启动,浏览器输入http://localhost:8001看到如下内容说明配置成功
3、创建服务提供者
cloud-demo右键->New->Module 重复第二步时的过程,只是选择下图项目创建
完成后打开pom.xml增加如下配置
<?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.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>admin-api</artifactId> <version>0.0.1-SNAPSHOT</version> <name>admin-api</name> <description>admin api</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <!--新增--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!--新增--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.yml配置
server: port: 8081 #端口 spring: application: name: customer-center #服务名称 eureka: client: service-url: defaultZone: http://localhost:8001/eureka/ #注册中心地址
启动,刷新注册中心
成功。
4、创建gateway服务
重复之前的创建过程,选择Spring Cloud Routing->GateWay
创建完成后pom.xml增加如下内容
<?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.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xxxxxx</groupId> <artifactId>getway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>getway</name> <description>getway</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <dependencies> <!--新增--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <exclusions> <exclusion> <artifactId>HikariCP</artifactId> <groupId>com.zaxxer</groupId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.yml
server: port: 8002 # 服务端口 spring: application: name: gateway-service # 服务名称 cloud: gateway: routes: - id: customer uri: lb://admin-api #eureka注册中心存在的服务名称 predicates: - Path=/api/customer/** #路径配置 filters: - StripPrefix=1 #忽略Path配置的个数,此处为1代表访问/api/customer/**时,会将api忽略,真实的访问地址为lb://admin-api/customer/**,如果为2,则为lb://admin-api/**
在customer-center中创建一个controller类提供一个简单的服务接口
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "customer") public class UserController { @GetMapping(value = "user") public String user(){ return "user"; } }
启动gateway服务
在浏览器中输入http://localhost:8002/api/customer/user和http://localhost:8081/customer/user得到的结果项目,说明配置成功
5、编写gateway过滤器
第一种:使用全局GlobalFilter过滤器
创建AuthorizeFilter类,只需要有@Component注解就可以了
package com.fulugame.filter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; 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.Mono; /** * Created by ShiJiaoYun on 2020/5/13. */ @Component @Slf4j public class AuthorizeFilter implements GlobalFilter, Ordered { private static final String AUTHORIZE_TOKEN = "token"; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { log.info("使用filter"); ServerHttpRequest request = exchange.getRequest(); HttpHeaders headers = request.getHeaders(); String token = headers.getFirst(AUTHORIZE_TOKEN); if (null == token){ token=request.getQueryParams().getFirst(AUTHORIZE_TOKEN); } ServerHttpResponse response = exchange.getResponse(); if (StringUtils.isEmpty(token)){ response.setStatusCode(HttpStatus.UNAUTHORIZED); return response.setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { return 0; } }
然后再通过http://localhost:8002/api/customer/user和http://localhost:8002/api/customer/user?token=12121访问就会有两种不同结果
这样同样可以实现过滤器效果