标签:point context repo cto put spring 重要 页面 als
1、mysql数据库表结构
2、pom.xml依赖
-
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-actuator</artifactId>
-
-
-
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-gateway</artifactId>
-
-
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-gateway-webflux</artifactId>
-
-
-
-
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
3、application.yml添加
-
-
-
-
-
-
-
-
4、核心配置类,加载路由配置到redis,官网核心包从redis获取路由配置,提高效率
-
import com.alibaba.fastjson.JSON;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.cloud.gateway.route.RouteDefinition;
-
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
-
import org.springframework.cloud.gateway.support.NotFoundException;
-
import org.springframework.data.redis.core.RedisTemplate;
-
import org.springframework.stereotype.Component;
-
import reactor.core.publisher.Flux;
-
import reactor.core.publisher.Mono;
-
-
import java.util.ArrayList;
-
-
-
-
-
-
-
public class RedisRouteDefinitionRepository implements RouteDefinitionRepository {
-
-
public static final String GATEWAY_ROUTES = "gateway:routes";
-
-
private RedisTemplate<String,Object> redisTemplate;
-
-
-
-
public Flux<RouteDefinition> getRouteDefinitions() {
-
List<RouteDefinition> routeDefinitions = new ArrayList<>();
-
redisTemplate.opsForHash().values(GATEWAY_ROUTES).stream().forEach(routeDefinition -> {
-
routeDefinitions.add(JSON.parseObject(routeDefinition.toString(), RouteDefinition.class));
-
-
return Flux.fromIterable(routeDefinitions);
-
-
-
-
public Mono<Void> save(Mono<RouteDefinition> route) {
-
-
.flatMap(routeDefinition -> {
-
redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(),
-
JSON.toJSONString(routeDefinition));
-
-
-
-
-
-
public Mono<Void> delete(Mono<String> routeId) {
-
return routeId.flatMap(id -> {
-
if (redisTemplate.opsForHash().hasKey(GATEWAY_ROUTES, id)) {
-
redisTemplate.opsForHash().delete(GATEWAY_ROUTES, id);
-
-
-
return Mono.defer(() -> Mono.error(new NotFoundException("路由文件没有找到: " + routeId)));
-
-
-
-
5、核心配置类,项目初始化加载数据库的路由配置到redis
-
-
-
import java.util.HashMap;
-
-
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.CommandLineRunner;
-
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
-
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.context.ApplicationEventPublisher;
-
import org.springframework.context.ApplicationEventPublisherAware;
-
import org.springframework.stereotype.Service;
-
import org.springframework.web.util.UriComponentsBuilder;
-
-
import com.alibaba.fastjson.JSON;
-
import com.smc.gateway.dao.GatewayRouteMapper;
-
import com.smc.gateway.entity.GatewayRoute;
-
import com.smc.gateway.repository.RedisRouteDefinitionRepository;
-
-
import lombok.extern.slf4j.Slf4j;
-
import reactor.core.publisher.Mono;
-
-
-
-
public class GatewayServiceHandler implements ApplicationEventPublisherAware, CommandLineRunner {
-
-
private RedisRouteDefinitionRepository routeDefinitionWriter;
-
private ApplicationEventPublisher publisher;
-
-
-
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
-
this.publisher = applicationEventPublisher;
-
-
-
-
-
private GatewayRouteMapper gatewayRouteMapper;
-
-
-
public void run(String... args){
-
-
-
-
public String loadRouteConfig() {
-
log.info("====开始加载=====网关配置信息=========");
-
-
redisTemplate.delete(RedisRouteDefinitionRepository.GATEWAY_ROUTES);
-
-
-
List<GatewayRoute> gatewayRouteList = gatewayRouteMapper.queryAllRoutes();
-
gatewayRouteList.forEach(gatewayRoute -> {
-
RouteDefinition definition=handleData(gatewayRoute);
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
-
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
log.info("=======网关配置信息===加载完成======");
-
-
-
-
public void saveRoute(GatewayRoute gatewayRoute){
-
RouteDefinition definition=handleData(gatewayRoute);
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
public void update(GatewayRoute gatewayRoute) {
-
RouteDefinition definition=handleData(gatewayRoute);
-
-
this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
-
-
-
-
public void deleteRoute(String routeId){
-
routeDefinitionWriter.delete(Mono.just(routeId)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
-
-
-
-
-
private RouteDefinition handleData(GatewayRoute gatewayRoute){
-
RouteDefinition definition = new RouteDefinition();
-
Map<String, String> predicateParams = new HashMap<>(8);
-
PredicateDefinition predicate = new PredicateDefinition();
-
FilterDefinition filterDefinition = new FilterDefinition();
-
Map<String, String> filterParams = new HashMap<>(8);
-
-
-
if(gatewayRoute.getUri().startsWith("http")){
-
-
uri = UriComponentsBuilder.fromHttpUrl(gatewayRoute.getUri()).build().toUri();
-
-
-
uri = UriComponentsBuilder.fromUriString("lb://"+gatewayRoute.getUri()).build().toUri();
-
-
-
definition.setId(gatewayRoute.getServiceId());
-
-
predicate.setName("Path");
-
predicateParams.put("pattern",gatewayRoute.getPredicates());
-
predicate.setArgs(predicateParams);
-
-
-
filterDefinition.setName("StripPrefix");
-
filterParams.put("_genkey_0", gatewayRoute.getFilters().toString());
-
filterDefinition.setArgs(filterParams);
-
-
definition.setPredicates(Arrays.asList(predicate));
-
definition.setFilters(Arrays.asList(filterDefinition));
-
-
definition.setOrder(Integer.parseInt(gatewayRoute.getGOrder()));
-
-
-
-
6、自定义controller层;
直接在数据库添加路由配置信息;
前端页面增、删、改路由配置信息,并刷新redis使配置信息立即生效;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.PathVariable;
-
import org.springframework.web.bind.annotation.PostMapping;
-
import org.springframework.web.bind.annotation.RequestBody;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
import org.springframework.beans.BeanUtils;
-
-
import com.smc.gateway.dto.GatewayRouteDto;
-
import com.smc.gateway.route.GatewayRouteService;
-
import com.smc.gateway.route.GatewayServiceHandler;
-
-
-
@RequestMapping("/route")
-
public class RouteController {
-
-
-
private GatewayServiceHandler gatewayServiceHandler;
-
-
-
private GatewayRouteService gatewayRouteService;
-
-
-
-
-
-
-
-
-
public String refresh() throws Exception {
-
return this.gatewayServiceHandler.loadRouteConfig();
-
-
-
-
-
-
-
-
-
-
public String add(@RequestBody GatewayRouteDto gatewayRouteDto) throws Exception {
-
gatewayRouteService.add(gatewayRouteDto);
-
GatewayRoute gatewayRoute=new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayServiceHandler.saveRoute(gatewayRoute);
-
-
-
-
-
public String update(@RequestBody GatewayRouteDto gatewayRouteDto) throws Exception {
-
gatewayRouteService.update(gatewayRouteDto);
-
GatewayRoute gatewayRoute=new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayServiceHandler.update(gatewayRoute);
-
-
-
-
-
public String delete(@PathVariable String id) throws Exception {
-
gatewayRouteService.delete(id);
-
gatewayServiceHandler.deleteRoute(id);
-
-
-
-
7、自定义service层,增、删、改、查数据库路由配置信息
-
-
-
import org.springframework.beans.BeanUtils;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
-
import com.smc.gateway.dao.GatewayRouteMapper;
-
import com.smc.gateway.dto.GatewayRouteDto;
-
import com.smc.gateway.entity.GatewayRoute;
-
-
-
public class GatewayRouteService {
-
-
-
private GatewayRouteMapper gatewayRouteMapper;
-
-
public Integer add(GatewayRouteDto gatewayRouteDto) {
-
GatewayRoute gatewayRoute = new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayRoute.setCreateDate(new Date());
-
gatewayRoute.setCreatorId("");
-
return gatewayRouteMapper.insertSelective(gatewayRoute);
-
-
-
public Integer update(GatewayRouteDto gatewayRouteDto) {
-
GatewayRoute gatewayRoute = new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayRoute.setUpdateDate(new Date());
-
gatewayRoute.setUpdateId("");
-
return gatewayRouteMapper.updateByPrimaryKeySelective(gatewayRoute);
-
-
-
public Integer delete(String id) {
-
return gatewayRouteMapper.deleteByPrimaryKey(Long.parseLong(id));
-
-
-
8、前端参数dto
-
import lombok.AllArgsConstructor;
-
-
-
import lombok.NoArgsConstructor;
-
-
-
-
-
-
public class GatewayRouteDto {
-
-
-
private String serviceId;
-
-
-
-
private String predicates;
-
-
-
-
-
-
-
9、数据库实体
-
-
import lombok.AllArgsConstructor;
-
-
-
import lombok.NoArgsConstructor;
-
-
-
-
-
-
-
-
-
public class GatewayRoute {
-
-
-
private String serviceId;
-
-
-
-
private String predicates;
-
-
-
-
-
-
private String creatorId;
-
-
-
-
-
-
-
-
-
-
-
10、访问地址
查看路由配置信息 http://localhost:18080/actuator/gateway/routes
手动刷新路由配置信息 http://localhost:18080/route/refresh
1、mysql数据库表结构
2、pom.xml依赖
-
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-actuator</artifactId>
-
-
-
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-gateway</artifactId>
-
-
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-gateway-webflux</artifactId>
-
-
-
-
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
3、application.yml添加
-
-
-
-
-
-
-
-
4、核心配置类,加载路由配置到redis,官网核心包从redis获取路由配置,提高效率
-
import com.alibaba.fastjson.JSON;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.cloud.gateway.route.RouteDefinition;
-
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
-
import org.springframework.cloud.gateway.support.NotFoundException;
-
import org.springframework.data.redis.core.RedisTemplate;
-
import org.springframework.stereotype.Component;
-
import reactor.core.publisher.Flux;
-
import reactor.core.publisher.Mono;
-
-
import java.util.ArrayList;
-
-
-
-
-
-
-
public class RedisRouteDefinitionRepository implements RouteDefinitionRepository {
-
-
public static final String GATEWAY_ROUTES = "gateway:routes";
-
-
private RedisTemplate<String,Object> redisTemplate;
-
-
-
-
public Flux<RouteDefinition> getRouteDefinitions() {
-
List<RouteDefinition> routeDefinitions = new ArrayList<>();
-
redisTemplate.opsForHash().values(GATEWAY_ROUTES).stream().forEach(routeDefinition -> {
-
routeDefinitions.add(JSON.parseObject(routeDefinition.toString(), RouteDefinition.class));
-
-
return Flux.fromIterable(routeDefinitions);
-
-
-
-
public Mono<Void> save(Mono<RouteDefinition> route) {
-
-
.flatMap(routeDefinition -> {
-
redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(),
-
JSON.toJSONString(routeDefinition));
-
-
-
-
-
-
public Mono<Void> delete(Mono<String> routeId) {
-
return routeId.flatMap(id -> {
-
if (redisTemplate.opsForHash().hasKey(GATEWAY_ROUTES, id)) {
-
redisTemplate.opsForHash().delete(GATEWAY_ROUTES, id);
-
-
-
return Mono.defer(() -> Mono.error(new NotFoundException("路由文件没有找到: " + routeId)));
-
-
-
-
5、核心配置类,项目初始化加载数据库的路由配置到redis
-
-
-
import java.util.HashMap;
-
-
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.CommandLineRunner;
-
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
-
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.context.ApplicationEventPublisher;
-
import org.springframework.context.ApplicationEventPublisherAware;
-
import org.springframework.stereotype.Service;
-
import org.springframework.web.util.UriComponentsBuilder;
-
-
import com.alibaba.fastjson.JSON;
-
import com.smc.gateway.dao.GatewayRouteMapper;
-
import com.smc.gateway.entity.GatewayRoute;
-
import com.smc.gateway.repository.RedisRouteDefinitionRepository;
-
-
import lombok.extern.slf4j.Slf4j;
-
import reactor.core.publisher.Mono;
-
-
-
-
public class GatewayServiceHandler implements ApplicationEventPublisherAware, CommandLineRunner {
-
-
private RedisRouteDefinitionRepository routeDefinitionWriter;
-
private ApplicationEventPublisher publisher;
-
-
-
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
-
this.publisher = applicationEventPublisher;
-
-
-
-
-
private GatewayRouteMapper gatewayRouteMapper;
-
-
-
public void run(String... args){
-
-
-
-
public String loadRouteConfig() {
-
log.info("====开始加载=====网关配置信息=========");
-
-
redisTemplate.delete(RedisRouteDefinitionRepository.GATEWAY_ROUTES);
-
-
-
List<GatewayRoute> gatewayRouteList = gatewayRouteMapper.queryAllRoutes();
-
gatewayRouteList.forEach(gatewayRoute -> {
-
RouteDefinition definition=handleData(gatewayRoute);
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
-
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
log.info("=======网关配置信息===加载完成======");
-
-
-
-
public void saveRoute(GatewayRoute gatewayRoute){
-
RouteDefinition definition=handleData(gatewayRoute);
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
public void update(GatewayRoute gatewayRoute) {
-
RouteDefinition definition=handleData(gatewayRoute);
-
-
this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
-
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
-
-
-
-
public void deleteRoute(String routeId){
-
routeDefinitionWriter.delete(Mono.just(routeId)).subscribe();
-
this.publisher.publishEvent(new RefreshRoutesEvent(this));
-
-
-
-
-
-
-
-
private RouteDefinition handleData(GatewayRoute gatewayRoute){
-
RouteDefinition definition = new RouteDefinition();
-
Map<String, String> predicateParams = new HashMap<>(8);
-
PredicateDefinition predicate = new PredicateDefinition();
-
FilterDefinition filterDefinition = new FilterDefinition();
-
Map<String, String> filterParams = new HashMap<>(8);
-
-
-
if(gatewayRoute.getUri().startsWith("http")){
-
-
uri = UriComponentsBuilder.fromHttpUrl(gatewayRoute.getUri()).build().toUri();
-
-
-
uri = UriComponentsBuilder.fromUriString("lb://"+gatewayRoute.getUri()).build().toUri();
-
-
-
definition.setId(gatewayRoute.getServiceId());
-
-
predicate.setName("Path");
-
predicateParams.put("pattern",gatewayRoute.getPredicates());
-
predicate.setArgs(predicateParams);
-
-
-
filterDefinition.setName("StripPrefix");
-
filterParams.put("_genkey_0", gatewayRoute.getFilters().toString());
-
filterDefinition.setArgs(filterParams);
-
-
definition.setPredicates(Arrays.asList(predicate));
-
definition.setFilters(Arrays.asList(filterDefinition));
-
-
definition.setOrder(Integer.parseInt(gatewayRoute.getGOrder()));
-
-
-
-
6、自定义controller层;
直接在数据库添加路由配置信息;
前端页面增、删、改路由配置信息,并刷新redis使配置信息立即生效;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.PathVariable;
-
import org.springframework.web.bind.annotation.PostMapping;
-
import org.springframework.web.bind.annotation.RequestBody;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
import org.springframework.beans.BeanUtils;
-
-
import com.smc.gateway.dto.GatewayRouteDto;
-
import com.smc.gateway.route.GatewayRouteService;
-
import com.smc.gateway.route.GatewayServiceHandler;
-
-
-
@RequestMapping("/route")
-
public class RouteController {
-
-
-
private GatewayServiceHandler gatewayServiceHandler;
-
-
-
private GatewayRouteService gatewayRouteService;
-
-
-
-
-
-
-
-
-
public String refresh() throws Exception {
-
return this.gatewayServiceHandler.loadRouteConfig();
-
-
-
-
-
-
-
-
-
-
public String add(@RequestBody GatewayRouteDto gatewayRouteDto) throws Exception {
-
gatewayRouteService.add(gatewayRouteDto);
-
GatewayRoute gatewayRoute=new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayServiceHandler.saveRoute(gatewayRoute);
-
-
-
-
-
public String update(@RequestBody GatewayRouteDto gatewayRouteDto) throws Exception {
-
gatewayRouteService.update(gatewayRouteDto);
-
GatewayRoute gatewayRoute=new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayServiceHandler.update(gatewayRoute);
-
-
-
-
-
public String delete(@PathVariable String id) throws Exception {
-
gatewayRouteService.delete(id);
-
gatewayServiceHandler.deleteRoute(id);
-
-
-
-
7、自定义service层,增、删、改、查数据库路由配置信息
-
-
-
import org.springframework.beans.BeanUtils;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
-
import com.smc.gateway.dao.GatewayRouteMapper;
-
import com.smc.gateway.dto.GatewayRouteDto;
-
import com.smc.gateway.entity.GatewayRoute;
-
-
-
public class GatewayRouteService {
-
-
-
private GatewayRouteMapper gatewayRouteMapper;
-
-
public Integer add(GatewayRouteDto gatewayRouteDto) {
-
GatewayRoute gatewayRoute = new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayRoute.setCreateDate(new Date());
-
gatewayRoute.setCreatorId("");
-
return gatewayRouteMapper.insertSelective(gatewayRoute);
-
-
-
public Integer update(GatewayRouteDto gatewayRouteDto) {
-
GatewayRoute gatewayRoute = new GatewayRoute();
-
BeanUtils.copyProperties(gatewayRouteDto, gatewayRoute);
-
gatewayRoute.setUpdateDate(new Date());
-
gatewayRoute.setUpdateId("");
-
return gatewayRouteMapper.updateByPrimaryKeySelective(gatewayRoute);
-
-
-
public Integer delete(String id) {
-
return gatewayRouteMapper.deleteByPrimaryKey(Long.parseLong(id));
-
-
-
8、前端参数dto
-
import lombok.AllArgsConstructor;
-
-
-
import lombok.NoArgsConstructor;
-
-
-
-
-
-
public class GatewayRouteDto {
-
-
-
private String serviceId;
-
-
-
-
private String predicates;
-
-
-
-
-
-
-
9、数据库实体
-
-
import lombok.AllArgsConstructor;
-
-
-
import lombok.NoArgsConstructor;
-
-
-
-
-
-
-
-
-
public class GatewayRoute {
-
-
-
private String serviceId;
-
-
-
-
private String predicates;
-
-
-
-
-
-
private String creatorId;
-
-
-
-
-
-
-
-
-
-
-
10、访问地址
查看路由配置信息 http://localhost:18080/actuator/gateway/routes
手动刷新路由配置信息 http://localhost:18080/route/refresh
转自:https://blog.csdn.net/qq_42714869/article/details/92794911
springcloud gateway动态路由实现,mysql存储配置
标签:point context repo cto put spring 重要 页面 als
原文地址:https://www.cnblogs.com/javalinux/p/14377494.html