初始化提交
This commit is contained in:
16
gateway/gateway-admin/.gitignore
vendored
Normal file
16
gateway/gateway-admin/.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
target/
|
||||
logs/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
33
gateway/gateway-admin/pom.xml
Normal file
33
gateway/gateway-admin/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>gateway-admin</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>gateway-admin</name>
|
||||
<description>Demo Gateway Admin project for Spring Cloud</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>webapp-parent</artifactId>
|
||||
<groupId>business.chaoran</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../webapps/webapp-parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alicp.jetcache</groupId>
|
||||
<artifactId>jetcache-starter-redis</artifactId>
|
||||
<version>2.5.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-gateway-core</artifactId>
|
||||
<version>2.1.0.RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
66
gateway/gateway-admin/src/main/db/db.sql
Normal file
66
gateway/gateway-admin/src/main/db/db.sql
Normal file
@ -0,0 +1,66 @@
|
||||
SET NAMES utf8;
|
||||
|
||||
DROP DATABASE IF EXISTS sc_gateway;
|
||||
CREATE DATABASE sc_gateway DEFAULT CHARSET utf8mb4;
|
||||
|
||||
USE sc_gateway;
|
||||
|
||||
-- 网关路由表
|
||||
DROP TABLE IF EXISTS gateway_route;
|
||||
CREATE TABLE gateway_route
|
||||
(
|
||||
id VARCHAR(20) PRIMARY KEY COMMENT 'id',
|
||||
route_id VARCHAR(100) NOT NULL COMMENT '路由id',
|
||||
uri VARCHAR(100) NOT NULL COMMENT 'uri路径',
|
||||
predicates TEXT NOT NULL COMMENT '判定器',
|
||||
filters TEXT COMMENT '过滤器',
|
||||
orders INT COMMENT '排序',
|
||||
description VARCHAR(500) COMMENT '描述',
|
||||
status VARCHAR(1) DEFAULT 'Y' COMMENT '状态:Y-有效,N-无效',
|
||||
created_time DATETIME NOT NULL DEFAULT now() COMMENT '创建时间',
|
||||
updated_time DATETIME NOT NULL DEFAULT now() COMMENT '更新时间',
|
||||
created_by VARCHAR(100) NOT NULL COMMENT '创建人',
|
||||
updated_by VARCHAR(100) NOT NULL COMMENT '更新人'
|
||||
) COMMENT '网关路由表';
|
||||
|
||||
CREATE UNIQUE INDEX ux_gateway_routes_uri ON gateway_route (uri);
|
||||
|
||||
|
||||
-- DML初始数据
|
||||
-- 路由数据
|
||||
INSERT INTO gateway_route (id, route_id, uri, predicates, filters, orders, description, status, created_time, updated_time, created_by, updated_by)
|
||||
VALUES
|
||||
(101,
|
||||
'authorization-server',
|
||||
'lb://authorization-server:8000',
|
||||
'[{"name":"Path","args":{"pattern":"/authorization-server/**"}}]',
|
||||
'[{"name":"StripPrefix","args":{"parts":"1"}}]',
|
||||
100,
|
||||
'授权认证服务网关注册',
|
||||
'Y', now(), now(), 'system', 'system'),
|
||||
(102,
|
||||
'authentication-server',
|
||||
'lb://authentication-server:8001',
|
||||
'[{"name":"Path","args":{"pattern":"/authentication-server/**"}}]',
|
||||
'[{"name":"StripPrefix","args":{"parts":"1"}}]',
|
||||
100,
|
||||
'签权服务网关注册',
|
||||
'Y', now(), now(), 'system', 'system'),
|
||||
(103,
|
||||
'organization',
|
||||
'lb://organization:8010',
|
||||
'[{"name":"Path","args":{"pattern":"/organization/**"}}]',
|
||||
'[{"name":"StripPrefix","args":{"parts":"1"}}]',
|
||||
100,
|
||||
'系统管理相关接口',
|
||||
'Y', now(), now(), 'system', 'system'),
|
||||
(104,
|
||||
'gateway-admin',
|
||||
'lb://gateway-admin:8445',
|
||||
'[{"name":"Path","args":{"pattern":"/gateway-admin/**"}}]',
|
||||
'[{"name":"StripPrefix","args":{"parts":"1"}}]',
|
||||
100,
|
||||
'网关管理相关接口',
|
||||
'Y', now(), now(), 'system', 'system')
|
||||
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
package com.springboot.cloud.gateway.admin;
|
||||
|
||||
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
|
||||
import com.alicp.jetcache.anno.config.EnableMethodCache;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = GatewayClassPathWarningAutoConfiguration.class)
|
||||
@EnableDiscoveryClient
|
||||
@EnableCircuitBreaker
|
||||
@EnableMethodCache(basePackages = "com.springboot.cloud")
|
||||
@EnableCreateCacheAnnotation
|
||||
public class GatewayAdminApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayAdminApplication.class, args);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.core.TopicExchange;
|
||||
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class BusConfig {
|
||||
|
||||
public static final String QUEUE_NAME = "event-gateway";
|
||||
public static final String EXCHANGE_NAME = "spring-boot-exchange";
|
||||
public static final String ROUTING_KEY = "gateway-route";
|
||||
|
||||
@Bean
|
||||
Queue queue() {
|
||||
log.info("queue name:{}", QUEUE_NAME);
|
||||
return new Queue(QUEUE_NAME, false);
|
||||
}
|
||||
|
||||
@Bean
|
||||
TopicExchange exchange() {
|
||||
log.info("exchange:{}", EXCHANGE_NAME);
|
||||
return new TopicExchange(EXCHANGE_NAME);
|
||||
}
|
||||
|
||||
@Bean
|
||||
Binding binding(Queue queue, TopicExchange exchange) {
|
||||
log.info("binding {} to {} with {}", queue, exchange, ROUTING_KEY);
|
||||
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageConverter messageConverter() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
return new ContentTypeDelegatingMessageConverter(new Jackson2JsonMessageConverter(objectMapper));
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import com.springboot.cloud.common.web.handler.PoMetaObjectHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyMetaObjectHandler extends PoMetaObjectHandler {
|
||||
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import com.springboot.cloud.common.web.redis.RedisConfig;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class MyRedisConfig extends RedisConfig {
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* 初使化Mybatis审计字段自动赋值的interceptor
|
||||
*/
|
||||
@EnableTransactionManagement
|
||||
@Configuration
|
||||
public class MybatisConfig {
|
||||
/**
|
||||
* 分页插件
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig {
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.springboot.cloud.gateway.admin"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("网关管理api")
|
||||
.description("网关管理")
|
||||
.termsOfServiceUrl("https://github.com/zhoutaoo/SpringCloud")
|
||||
.version("2.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.springboot.cloud.gateway.admin.config;
|
||||
|
||||
import com.springboot.cloud.common.web.interceptor.UserInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebServerMvcConfigurerAdapter implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public HandlerInterceptor userInterceptor() {
|
||||
return new UserInterceptor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(userInterceptor());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package com.springboot.cloud.gateway.admin.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Mapper
|
||||
@Repository
|
||||
public interface GatewayRouteMapper extends BaseMapper<GatewayRoute> {
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.form;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.springboot.cloud.common.web.entity.form.BaseForm;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.FilterDefinition;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.PredicateDefinition;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel
|
||||
@Data
|
||||
@Slf4j
|
||||
public class GatewayRouteForm extends BaseForm<GatewayRoute> {
|
||||
|
||||
@NotEmpty(message = "网关断言不能为空")
|
||||
@ApiModelProperty(value = "网关断言")
|
||||
private List<PredicateDefinition> predicates = new ArrayList<>();
|
||||
|
||||
@ApiModelProperty(value = "网关过滤器信息")
|
||||
private List<FilterDefinition> filters = new ArrayList<>();
|
||||
|
||||
@NotBlank(message = "uri不能为空")
|
||||
@ApiModelProperty(value = "网关uri")
|
||||
private String uri;
|
||||
|
||||
@NotBlank(message = "路由id不能为空")
|
||||
@ApiModelProperty(value = "网关路由id")
|
||||
private String routeId;
|
||||
|
||||
@ApiModelProperty(value = "排序")
|
||||
private Integer orders = 0;
|
||||
|
||||
@ApiModelProperty(value = "网关路由描述信息")
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public GatewayRoute toPo(Class<GatewayRoute> clazz) {
|
||||
GatewayRoute gatewayRoute = new GatewayRoute();
|
||||
BeanUtils.copyProperties(this, gatewayRoute);
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
gatewayRoute.setFilters(objectMapper.writeValueAsString(this.getFilters()));
|
||||
gatewayRoute.setPredicates(objectMapper.writeValueAsString(this.getPredicates()));
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("网关filter或predicates配置转换异常", e);
|
||||
}
|
||||
return gatewayRoute;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.form;
|
||||
|
||||
import com.springboot.cloud.common.web.entity.form.BaseQueryForm;
|
||||
import com.springboot.cloud.gateway.admin.entity.param.GatewayRouteQueryParam;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import javax.validation.constraints.Past;
|
||||
import java.util.Date;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ApiModel
|
||||
@Data
|
||||
public class GatewayRouteQueryForm extends BaseQueryForm<GatewayRouteQueryParam> {
|
||||
|
||||
@ApiModelProperty(value = "uri路径", required = true)
|
||||
private String uri;
|
||||
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
|
||||
@Past(message = "查询开始时间必须小于当前日期")
|
||||
@ApiModelProperty(value = "查询开始时间")
|
||||
private Date createdTimeStart;
|
||||
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
|
||||
@Past(message = "查询结束时间必须小于当前日期")
|
||||
@ApiModelProperty(value = "查询结束时间")
|
||||
private Date createdTimeEnd;
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.ov;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.springboot.cloud.common.web.entity.vo.BaseVo;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.FilterDefinition;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.PredicateDefinition;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Slf4j
|
||||
public class GatewayRouteVo extends BaseVo {
|
||||
private String id;
|
||||
private String routeId;
|
||||
private String description;
|
||||
private String status;
|
||||
private String uri;
|
||||
private Integer orders;
|
||||
private String createdBy;
|
||||
private Date createdTime;
|
||||
private String updatedBy;
|
||||
private Date updatedTime;
|
||||
private List<FilterDefinition> filters = new ArrayList<>();
|
||||
private List<PredicateDefinition> predicates = new ArrayList<>();
|
||||
|
||||
public GatewayRouteVo(GatewayRoute gatewayRoute) {
|
||||
this.id = gatewayRoute.getId();
|
||||
this.routeId = gatewayRoute.getRouteId();
|
||||
this.uri = gatewayRoute.getUri();
|
||||
this.description = gatewayRoute.getDescription();
|
||||
this.status = gatewayRoute.getStatus();
|
||||
this.orders = gatewayRoute.getOrders();
|
||||
this.createdBy = gatewayRoute.getCreatedBy();
|
||||
this.createdTime = gatewayRoute.getCreatedTime();
|
||||
this.updatedBy = gatewayRoute.getUpdatedBy();
|
||||
this.updatedTime = gatewayRoute.getUpdatedTime();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
this.filters = objectMapper.readValue(gatewayRoute.getFilters(), new TypeReference<List<FilterDefinition>>() {
|
||||
});
|
||||
this.predicates = objectMapper.readValue(gatewayRoute.getPredicates(), new TypeReference<List<PredicateDefinition>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
log.error("网关路由对象转换失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.param;
|
||||
|
||||
import com.springboot.cloud.common.web.entity.param.BaseParam;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class GatewayRouteQueryParam extends BaseParam<GatewayRoute> {
|
||||
private String uri;
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.po;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FilterDefinition {
|
||||
private String name;
|
||||
private Map<String, String> args = new LinkedHashMap<>();
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.po;
|
||||
|
||||
import com.springboot.cloud.common.web.entity.po.BasePo;
|
||||
import lombok.*;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class GatewayRoute extends BasePo {
|
||||
private String uri;
|
||||
private String routeId;
|
||||
private String predicates;
|
||||
private String filters;
|
||||
private String description;
|
||||
private Integer orders = 0;
|
||||
private String status = "Y";
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package com.springboot.cloud.gateway.admin.entity.po;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@EqualsAndHashCode
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PredicateDefinition {
|
||||
private String name;
|
||||
private Map<String, String> args = new LinkedHashMap<>();
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package com.springboot.cloud.gateway.admin.events;
|
||||
|
||||
import com.springboot.cloud.gateway.admin.config.BusConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EventSender {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
@Autowired
|
||||
private MessageConverter messageConverter;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setMessageConverter(messageConverter);
|
||||
}
|
||||
|
||||
public void send(String routingKey, Object object) {
|
||||
log.info("routingKey:{}=>message:{}", routingKey, object);
|
||||
rabbitTemplate.convertAndSend(BusConfig.EXCHANGE_NAME, routingKey, object);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.springboot.cloud.gateway.admin.exception;
|
||||
|
||||
import com.springboot.cloud.common.web.exception.DefaultGlobalExceptionHandlerAdvice;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandlerAdvice extends DefaultGlobalExceptionHandlerAdvice {
|
||||
}
|
||||
@ -0,0 +1,91 @@
|
||||
package com.springboot.cloud.gateway.admin.rest;
|
||||
|
||||
import com.springboot.cloud.common.core.entity.vo.Result;
|
||||
import com.springboot.cloud.gateway.admin.entity.form.GatewayRouteForm;
|
||||
import com.springboot.cloud.gateway.admin.entity.form.GatewayRouteQueryForm;
|
||||
import com.springboot.cloud.gateway.admin.entity.ov.GatewayRouteVo;
|
||||
import com.springboot.cloud.gateway.admin.entity.param.GatewayRouteQueryParam;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import com.springboot.cloud.gateway.admin.service.IGatewayRouteService;
|
||||
import io.swagger.annotations.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/gateway/routes")
|
||||
@Api("gateway routes")
|
||||
@Slf4j
|
||||
public class GatewayRouteController {
|
||||
|
||||
@Autowired
|
||||
private IGatewayRouteService gatewayRoutService;
|
||||
|
||||
@ApiOperation(value = "新增网关路由", notes = "新增一个网关路由")
|
||||
@ApiImplicitParam(name = "gatewayRoutForm", value = "新增网关路由form表单", required = true, dataType = "GatewayRouteForm")
|
||||
@PostMapping
|
||||
public Result add(@Valid @RequestBody GatewayRouteForm gatewayRoutForm) {
|
||||
log.info("name:", gatewayRoutForm);
|
||||
GatewayRoute gatewayRout = gatewayRoutForm.toPo(GatewayRoute.class);
|
||||
return Result.success(gatewayRoutService.add(gatewayRout));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除网关路由", notes = "根据url的id来指定删除对象")
|
||||
@ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "string")
|
||||
@DeleteMapping(value = "/{id}")
|
||||
public Result delete(@PathVariable String id) {
|
||||
return Result.success(gatewayRoutService.delete(id));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "修改网关路由", notes = "修改指定网关路由信息")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "id", value = "网关路由ID", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(name = "gatewayRoutForm", value = "网关路由实体", required = true, dataType = "GatewayRouteForm")
|
||||
})
|
||||
@PutMapping(value = "/{id}")
|
||||
public Result update(@PathVariable String id, @Valid @RequestBody GatewayRouteForm gatewayRoutForm) {
|
||||
GatewayRoute gatewayRout = gatewayRoutForm.toPo(GatewayRoute.class);
|
||||
gatewayRout.setId(id);
|
||||
return Result.success(gatewayRoutService.update(gatewayRout));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取网关路由", notes = "根据id获取指定网关路由信息")
|
||||
@ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "string")
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result get(@PathVariable String id) {
|
||||
log.info("get with id:{}", id);
|
||||
return Result.success(new GatewayRouteVo(gatewayRoutService.get(id)));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "根据uri获取网关路由", notes = "根据uri获取网关路由信息,简单查询")
|
||||
@ApiImplicitParam(paramType = "query", name = "name", value = "网关路由路径", required = true, dataType = "string")
|
||||
@ApiResponses(
|
||||
@ApiResponse(code = 200, message = "处理成功", response = Result.class)
|
||||
)
|
||||
@GetMapping
|
||||
public Result getByUri(@RequestParam String uri) {
|
||||
return Result.success(gatewayRoutService.query(new GatewayRouteQueryParam(uri)).stream().findFirst());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "搜索网关路由", notes = "根据条件查询网关路由信息")
|
||||
@ApiImplicitParam(name = "gatewayRoutQueryForm", value = "网关路由查询参数", required = true, dataType = "GatewayRouteQueryForm")
|
||||
@ApiResponses(
|
||||
@ApiResponse(code = 200, message = "处理成功", response = Result.class)
|
||||
)
|
||||
@PostMapping(value = "/conditions")
|
||||
public Result search(@Valid @RequestBody GatewayRouteQueryForm gatewayRouteQueryForm) {
|
||||
return Result.success(gatewayRoutService.query(gatewayRouteQueryForm.toParam(GatewayRouteQueryParam.class)));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "重载网关路由", notes = "将所以网关的路由全部重载到redis中")
|
||||
@ApiResponses(
|
||||
@ApiResponse(code = 200, message = "处理成功", response = Result.class)
|
||||
)
|
||||
@PostMapping(value = "/overload")
|
||||
public Result overload() {
|
||||
return Result.success(gatewayRoutService.overload());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package com.springboot.cloud.gateway.admin.service;
|
||||
|
||||
import com.springboot.cloud.gateway.admin.entity.ov.GatewayRouteVo;
|
||||
import com.springboot.cloud.gateway.admin.entity.param.GatewayRouteQueryParam;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IGatewayRouteService {
|
||||
/**
|
||||
* 获取网关路由
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
GatewayRoute get(String id);
|
||||
|
||||
/**
|
||||
* 新增网关路由
|
||||
*
|
||||
* @param gatewayRoute
|
||||
* @return
|
||||
*/
|
||||
boolean add(GatewayRoute gatewayRoute);
|
||||
|
||||
/**
|
||||
* 查询网关路由
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<GatewayRouteVo> query(GatewayRouteQueryParam gatewayRouteQueryParam);
|
||||
|
||||
/**
|
||||
* 更新网关路由信息
|
||||
*
|
||||
* @param gatewayRoute
|
||||
*/
|
||||
boolean update(GatewayRoute gatewayRoute);
|
||||
|
||||
/**
|
||||
* 根据id删除网关路由
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
boolean delete(String id);
|
||||
|
||||
/**
|
||||
* 重新加载网关路由配置到redis
|
||||
*
|
||||
* @return 成功返回true
|
||||
*/
|
||||
boolean overload();
|
||||
}
|
||||
@ -0,0 +1,117 @@
|
||||
package com.springboot.cloud.gateway.admin.service.impl;
|
||||
|
||||
import com.alicp.jetcache.Cache;
|
||||
import com.alicp.jetcache.anno.CacheType;
|
||||
import com.alicp.jetcache.anno.CreateCache;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.springboot.cloud.gateway.admin.config.BusConfig;
|
||||
import com.springboot.cloud.gateway.admin.dao.GatewayRouteMapper;
|
||||
import com.springboot.cloud.gateway.admin.entity.ov.GatewayRouteVo;
|
||||
import com.springboot.cloud.gateway.admin.entity.param.GatewayRouteQueryParam;
|
||||
import com.springboot.cloud.gateway.admin.entity.po.GatewayRoute;
|
||||
import com.springboot.cloud.gateway.admin.events.EventSender;
|
||||
import com.springboot.cloud.gateway.admin.service.IGatewayRouteService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class GatewayRouteService extends ServiceImpl<GatewayRouteMapper, GatewayRoute> implements IGatewayRouteService {
|
||||
|
||||
private static final String GATEWAY_ROUTES = "gateway_routes::";
|
||||
|
||||
@CreateCache(name = GATEWAY_ROUTES, cacheType = CacheType.REMOTE)
|
||||
private Cache<String, RouteDefinition> gatewayRouteCache;
|
||||
|
||||
@Autowired
|
||||
private EventSender eventSender;
|
||||
|
||||
@Override
|
||||
public boolean add(GatewayRoute gatewayRoute) {
|
||||
boolean isSuccess = this.save(gatewayRoute);
|
||||
// 转化为gateway需要的类型,缓存到redis, 并事件通知各网关应用
|
||||
RouteDefinition routeDefinition = gatewayRouteToRouteDefinition(gatewayRoute);
|
||||
gatewayRouteCache.put(gatewayRoute.getRouteId(), routeDefinition);
|
||||
eventSender.send(BusConfig.ROUTING_KEY, routeDefinition);
|
||||
return isSuccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(String id) {
|
||||
GatewayRoute gatewayRoute = this.getById(id);
|
||||
// 删除redis缓存, 并事件通知各网关应用
|
||||
gatewayRouteCache.remove(gatewayRoute.getRouteId());
|
||||
eventSender.send(BusConfig.ROUTING_KEY, gatewayRouteToRouteDefinition(gatewayRoute));
|
||||
return this.removeById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update(GatewayRoute gatewayRoute) {
|
||||
boolean isSuccess = this.updateById(gatewayRoute);
|
||||
// 转化为gateway需要的类型,缓存到redis, 并事件通知各网关应用
|
||||
RouteDefinition routeDefinition = gatewayRouteToRouteDefinition(gatewayRoute);
|
||||
gatewayRouteCache.put(gatewayRoute.getRouteId(), routeDefinition);
|
||||
eventSender.send(BusConfig.ROUTING_KEY, routeDefinition);
|
||||
return isSuccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据库中json对象转换为网关需要的RouteDefinition对象
|
||||
*
|
||||
* @param gatewayRoute
|
||||
* @return RouteDefinition
|
||||
*/
|
||||
private RouteDefinition gatewayRouteToRouteDefinition(GatewayRoute gatewayRoute) {
|
||||
RouteDefinition routeDefinition = new RouteDefinition();
|
||||
routeDefinition.setId(gatewayRoute.getRouteId());
|
||||
routeDefinition.setOrder(gatewayRoute.getOrders());
|
||||
routeDefinition.setUri(URI.create(gatewayRoute.getUri()));
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
routeDefinition.setFilters(objectMapper.readValue(gatewayRoute.getFilters(), new TypeReference<List<FilterDefinition>>() {
|
||||
}));
|
||||
routeDefinition.setPredicates(objectMapper.readValue(gatewayRoute.getPredicates(), new TypeReference<List<PredicateDefinition>>() {
|
||||
}));
|
||||
} catch (IOException e) {
|
||||
log.error("网关路由对象转换失败", e);
|
||||
}
|
||||
return routeDefinition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayRoute get(String id) {
|
||||
return this.getById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GatewayRouteVo> query(GatewayRouteQueryParam gatewayRouteQueryParam) {
|
||||
QueryWrapper<GatewayRoute> queryWrapper = gatewayRouteQueryParam.build();
|
||||
queryWrapper.eq(StringUtils.isNotBlank(gatewayRouteQueryParam.getUri()), "uri", gatewayRouteQueryParam.getUri());
|
||||
return this.list(queryWrapper).stream().map(GatewayRouteVo::new).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostConstruct
|
||||
public boolean overload() {
|
||||
List<GatewayRoute> gatewayRoutes = this.list(new QueryWrapper<>());
|
||||
gatewayRoutes.forEach(gatewayRoute ->
|
||||
gatewayRouteCache.put(gatewayRoute.getRouteId(), gatewayRouteToRouteDefinition(gatewayRoute))
|
||||
);
|
||||
log.info("全局初使化网关路由成功!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package com.springboot.cloud.gateway.admin;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class GatewayAdminApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user