1、创建一个maven项目
next
Next
2、在创建好的maven项目上右键New->Module
选择Spring initializr创建eureka注册中心
Next
Next
Next
打开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注解就可以了
package com.swift.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.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增加如下配置
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.swift
customercenter
0.0.1-SNAPSHOT
customercenter
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
配置文件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增加如下内容
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.swift
gateway
0.0.1-SNAPSHOT
gateway
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
配置文件application.yml
server:
port: 8002 #端口
spring:
application:
name: gateway-service #服务名
cloud:
gateway:
routes:
- id: customer
uri: lb://customer-center #eureka注册中心存在的服务名称
predicates:
- Path=/api/customer/** #路径配置
filters:
- StripPrefix=1 #忽略Path配置的个数,此处为1代表访问/api/customer/**时,会将api忽略,真实的访问地址为lb://customer-center/customer/**,如果为2,则为lb://customer-center/**
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/ #注册中心地址
在customer-center中创建一个controller类提供一个简单的服务接口
package com.swift.customercenter.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @ClassName UserController
* @Description TODO
* @Date 2019-06-05 10:17
* @Version 1.0
*/
@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.swift.gateway.filter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author
* @ClassName AuthorizeFilter
* @Description 全局token校验器
* @Date 2019-06-05 14:09
* @Version 1.0
*/
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
private static final Log logger = LogFactory.getLog(AuthorizeFilter.class);
private static final String AUTHORIZE_TOKEN="token";
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
logger.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访问就会有两种不同结果
第二种:继承AbstractGatewayFilterFactory实现指定使用过滤器
创建类AuthorizeGatewayFilterFactory
package com.swift.gateway.filter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.PrefixPathGatewayFilterFactory;
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.util.StringUtils;
/**
* @author
* @ClassName AuthorizeGatewayFilterFactory
* @Description TODO
* @Date 2019-06-06 10:10
* @Version 1.0
*/
@Component
public class AuthorizeGatewayFilterFactory extends AbstractGatewayFilterFactory {
private static final Log logger = LogFactory.getLog(AbstractGatewayFilterFactory.class);
public AuthorizeGatewayFilterFactory(){
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
ServerHttpRequest request =exchange.getRequest();
String token = request.getHeaders().getFirst("token");
logger.info("token:"+token);
if (null == token){
token = request.getQueryParams().getFirst("token");
}
ServerHttpResponse response = exchange.getResponse();
if (StringUtils.isEmpty(token)){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
return chain.filter(exchange);
};
}
public static class Config{
static String s="123456";
}
}
修改application.yml配置
server:
port: 8002
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: customer
uri: lb://customer-center
predicates:
- Path=/api/customer/**
filters:
- StripPrefix=1
- Authorize=true #启用过滤器 Authorize为过滤类的前缀
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
这样同样可以实现过滤器效果