欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud Gateway使用redis實(shí)現(xiàn)動態(tài)路由的方法

 更新時間:2021年01月09日 10:19:53   作者:一一可可  
這篇文章主要介紹了SpringCloud Gateway使用redis實(shí)現(xiàn)動態(tài)路由的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 將 actuator 端點(diǎn)暴露出來

management:

endpoints:

web:

exposure:

include: "*"

2. redis 配置

http://www.dbjr.com.cn/article/203766.htm

3. 將原內(nèi)存路由持久化到 redis

@Component

public class RedisRouteDefinitionRepository implements RouteDefinitionRepository {

/**

* hash存儲的key

*/

public static final String GATEWAY_ROUTES = "gateway_dynamic_route";

@Resource

private StringRedisTemplate redisTemplate;

/**

* 獲取路由信息

* @return

*/

@Override

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);

}

@Override

public Mono<Void> save(Mono<RouteDefinition> route) {

return route.flatMap(routeDefinition -> {

redisTemplate.opsForHash().put(GATEWAY_ROUTES, routeDefinition.getId(), JSONObject.toJSONString(routeDefinition));

return Mono.empty();

});

}

@Override

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.empty();

}

return Mono.defer(() -> Mono.error(new NotFoundException("route definition is not found, routeId:" + routeId)));

});

}

}

4. 重寫動態(tài)路由服務(wù)

@Service

public class GatewayDynamicRouteService implements ApplicationEventPublisherAware {

@Resource

private RedisRouteDefinitionRepository redisRouteDefinitionRepository;

private ApplicationEventPublisher applicationEventPublisher;

/**

* 增加路由

* @param routeDefinition

* @return

*/

public int add(RouteDefinition routeDefinition) {

redisRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe();

applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));

return 1;

}

/**

* 更新

* @param routeDefinition

* @return

*/

public int update(RouteDefinition routeDefinition) {

redisRouteDefinitionRepository.delete(Mono.just(routeDefinition.getId()));

redisRouteDefinitionRepository.save(Mono.just(routeDefinition)).subscribe();

applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));

return 1;

}

/**

* 刪除

* @param id

* @return

*/

public Mono<ResponseEntity<Object>> delete(String id) {

return redisRouteDefinitionRepository.delete(Mono.just(id)).then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))

.onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));

}

@Override

public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {

this.applicationEventPublisher = applicationEventPublisher;

}

}

5. 對外暴露接口

@RestController

@RequestMapping("/gateway")

public class GatewayDynamicRouteController {

@Resource

private GatewayDynamicRouteService gatewayDynamicRouteService;

@PostMapping("/add")

public String create(@RequestBody RouteDefinition entity) {

int result = gatewayDynamicRouteService.add(entity);

return String.valueOf(result);

}

@PostMapping("/update")

public String update(@RequestBody RouteDefinition entity) {

int result = gatewayDynamicRouteService.update(entity);

return String.valueOf(result);

}

@DeleteMapping("/delete/{id}")

public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {

return gatewayDynamicRouteService.delete(id);

}

}

測試

測試前刪除我們配置的靜態(tài)路由,因?yàn)殪o態(tài)路由和 redis 動態(tài)路由同時存在時取并集。

訪問 http://localhost:2000/actuator/gateway/routes , 可以看到只有默認(rèn)路由。

[

{

"route_id": "CompositeDiscoveryClient_consul",

"route_definition": {

"id": "CompositeDiscoveryClient_consul",

"predicates": [

{

"name": "Path",

"args": {

"pattern": "/consul/**"

}

}

],

"filters": [

{

"name": "RewritePath",

"args": {

"regexp": "/consul/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://consul",

"order": 0

},

"order": 0

},

{

"route_id": "CompositeDiscoveryClient_idc-gateway",

"route_definition": {

"id": "CompositeDiscoveryClient_idc-gateway",

"predicates": [

{

"name": "Path",

"args": {

"pattern": "/idc-gateway/**"

}

}

],

"filters": [

{

"name": "RewritePath",

"args": {

"regexp": "/idc-gateway/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-gateway",

"order": 0

},

"order": 0

},

{

"route_id": "CompositeDiscoveryClient_idc-provider1",

"route_definition": {

"id": "CompositeDiscoveryClient_idc-provider1",

"predicates": [

{

"name": "Path",

"args": {

"pattern": "/idc-provider1/**"

}

}

],

"filters": [

{

"name": "RewritePath",

"args": {

"regexp": "/idc-provider1/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-provider1",

"order": 0

},

"order": 0

},

{

"route_id": "CompositeDiscoveryClient_idc-provider2",

"route_definition": {

"id": "CompositeDiscoveryClient_idc-provider2",

"predicates": [

{

"name": "Path",

"args": {

"pattern": "/idc-provider2/**"

}

}

],

"filters": [

{

"name": "RewritePath",

"args": {

"regexp": "/idc-provider2/(?<remaining>.*)",

"replacement": "/${remaining}"

}

}

],

"uri": "lb://idc-provider2",

"order": 0

},

"order": 0

}

]

這個時候訪問 http://192.168.124.5:2000/idc-provider1/provider1/1 根據(jù)結(jié)果可以推測能正確路由到 provider1, 測試結(jié)果一致。

創(chuàng)建 provider1 路由,將路徑設(shè)置為 /p1/**,測試是否生效。

POST 請求 http://localhost:2000/gateway/add

{

"id":"provider1",

"predicates":[

{

"name":"Path",

"args":{

"_genkey_0":"/p1/**"

}

},

{

"name":"RemoteAddr",

"args":{

"_genkey_0":"192.168.124.5/16"

}

}

],

"filters":[

{

"name":"StripPrefix",

"args":{

"_genkey_0":"1"

}

}

],

"uri":"lb://idc-provider1",

"order":0

}

查看 redis 存儲,或者請求 http://localhost:2000/actuator/gateway/routes  , 都可以看到配置成功。

到此這篇關(guān)于SpringCloud Gateway使用redis實(shí)現(xiàn)動態(tài)路由的文章就介紹到這了,更多相關(guān)SpringCloud Gateway動態(tài)路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java畢業(yè)設(shè)計實(shí)戰(zhàn)之線上水果超市商城的實(shí)現(xiàn)

    Java畢業(yè)設(shè)計實(shí)戰(zhàn)之線上水果超市商城的實(shí)現(xiàn)

    這是一個使用了java+SSM+springboot+redis開發(fā)的網(wǎng)上水果超市商城,是一個畢業(yè)設(shè)計的實(shí)戰(zhàn)練習(xí),具有水果超市商城該有的所有功能,感興趣的朋友快來看看吧
    2022-01-01
  • Java之如何獲取泛型參數(shù)

    Java之如何獲取泛型參數(shù)

    在Java開發(fā)中,獲取泛型參數(shù)一般有兩種方法:第一種是通過JDK自帶的API,主要利用反射機(jī)制來獲取類的泛型信息;第二種方法是借助Spring框架提供的GenericTypeResolver工具類,這種方式更加簡便,這兩種方法都能有效地幫助開發(fā)者在運(yùn)行時獲取到泛型參數(shù)
    2024-09-09
  • java實(shí)現(xiàn)滑動驗(yàn)證解鎖

    java實(shí)現(xiàn)滑動驗(yàn)證解鎖

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)滑動驗(yàn)證解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Flink支持哪些數(shù)據(jù)類型?

    Flink支持哪些數(shù)據(jù)類型?

    Apache Flink 以其獨(dú)特的方式來處理數(shù)據(jù)類型以及序列化,這種方式包括它自身的類型描述符、泛型類型提取以及類型序列化框架.本文檔描述了它們背后的概念和基本原理,需要的朋友可以參考下
    2021-06-06
  • Javaweb使用cors完成跨域ajax數(shù)據(jù)交互

    Javaweb使用cors完成跨域ajax數(shù)據(jù)交互

    本文由跨域、cors的概念開始,進(jìn)而向大家介紹了Javaweb使用cors完成跨域ajax數(shù)據(jù)交互的相關(guān)內(nèi)容,需要的朋友可以了解下。
    2017-09-09
  • 詳解Java 信號量Semaphore

    詳解Java 信號量Semaphore

    這篇文章主要介紹了Java 信號量Semaphore的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java并發(fā),感興趣的朋友可以了解下
    2020-09-09
  • 詳解關(guān)于spring bean名稱命名的那些事

    詳解關(guān)于spring bean名稱命名的那些事

    每個bean都有一個或者多個標(biāo)識符,這些標(biāo)識符在容器中必須是唯一的,這篇文章主要給大家介紹了關(guān)于spring bean名稱命名的那些事,需要的朋友可以參考下
    2021-07-07
  • 詳解怎么用Java的super關(guān)鍵字

    詳解怎么用Java的super關(guān)鍵字

    今天帶大家學(xué)習(xí)Java中super關(guān)鍵字是怎么用的,文中有非常詳細(xì)的介紹,對正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • java語言自行實(shí)現(xiàn)ULID過程底層原理詳解

    java語言自行實(shí)現(xiàn)ULID過程底層原理詳解

    這篇文章主要為大家介紹了java語言自行實(shí)現(xiàn)ULID過程底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java數(shù)據(jù)結(jié)構(gòu)超詳細(xì)分析二叉搜索樹

    Java數(shù)據(jù)結(jié)構(gòu)超詳細(xì)分析二叉搜索樹

    二叉搜索樹是以一棵二叉樹來組織的。每個節(jié)點(diǎn)是一個對象,包含的屬性有l(wèi)eft,right,p和key,其中,left指向該節(jié)點(diǎn)的左孩子,right指向該節(jié)點(diǎn)的右孩子,p指向該節(jié)點(diǎn)的父節(jié)點(diǎn),key是它的值
    2022-03-03

最新評論