<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    spring-gateway基于數據庫 + nacos 的動態路由

    一顆小胡椒2022-07-27 17:52:01

    動態路由的實現方式多種多樣,研究一下基于數據方式的動態路由。

    1. 創建項目,并pom.xml文件引入如下依賴

    <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>
     <groupId>com.olive</groupId>
     <artifactId>olive-gateway</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.7.1</version>
     </parent>
     <dependencyManagement>
      <dependencies>
       <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2021.0.3</version>
        <type>pom</type>
        <scope>import</scope>
       </dependency>
       <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2021.1</version>
        <type>pom</type>
        <scope>import</scope>
       </dependency>
      </dependencies>
     </dependencyManagement>
     <dependencies>
      <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-gateway</artifactId>
      </dependency>
      <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
      </dependency>
     </dependencies>
    </project>
    

    使用 spring-boot-starter-paren t就不需要給子集pom配置版本號了,因為它包括了

    • 定義了 java 編譯版本為 1.8
    • 使用 utf-8 格式編碼
    • 繼承 spring-boot-dependencies 進行統一版本依賴管理
    • 執行打包 war jar 操作配置;可以省略打包 plugin 的配置
    • 自動化資源過濾。如 application.properties 和 application.yml 的資源過濾、 包括 profile 多環境配置的
    • 自動化插件配置
    • 不需要配置 maven 打包 plugin 插件配置

    2. 從數據庫加載路由配置

    先定義一個接口,該接口的功能主要是返回數據庫配置的所有路由

    import org.springframework.cloud.gateway.route.RouteDefinition;
    import reactor.core.publisher.Flux;
    /**
     * 返回所有路由數據
     */
    public interface GatewayRouterService {
        Flux<RouteDefinition> getGatewayRoutes();
    }
    

    實現該接口

    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.cloud.gateway.route.RouteDefinition;
    import org.springframework.stereotype.Service;
    import com.olive.router.GatewayRouterService;
    import reactor.core.publisher.Flux;
    @Service
    public class RouterServiceImpl implements GatewayRouterService {
     @Override
     public Flux<RouteDefinition> getGatewayRoutes() {
         System.out.println("------getGatewayRoutes-------");
         List<RouteDefinition> routes = null;
         //TODO查詢數據庫返回所有有效路由
         return Flux.fromIterable(routes );
     }
    }
    

    3. 動態加載路由

    實現 RouteDefinitionRepository 接口,Spring自動從數據庫中讀取路由配置;采用 nacos 作為服務發現與配置中心,nacos 自動觸發心跳檢測,網關基于心跳檢測會自動刷新數據庫路由配置,默認 30s 進行一次路由刷新。參考實現 RouteRefreshListener。

    import org.springframework.cloud.gateway.route.RouteDefinition;
    import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
    import com.olive.router.GatewayRouterService;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    public class DatabaseRouteDefinitionRepository implements RouteDefinitionRepository {
        private final GatewayRouterService gatewayRouterService;
        public InDBRouteDefinitionRepository(GatewayRouterService gatewayRouterService) {
            this.gatewayRouterService= gatewayRouterService;
        }
        @Override
        public Flux<RouteDefinition> getRouteDefinitions() {
            return gatewayRouterService.getGatewayRoutes();
        }
        @Override
        public Mono<Void> save(Mono<RouteDefinition> route) {
            //TODO
            return Mono.empty();
        }
        @Override
        public Mono<Void> delete(Mono<String> routeId) {
            //TODO
            return Mono.empty();
        }
    }
    

    4. 配置加載自定義的路由

    spring-gateway 默認是先從 application.yml 文件加載路由配置;這里通過 AutoConfigureBefore 注解,加載數據庫的路由配置。

    import org.springframework.boot.autoconfigure.AutoConfigureBefore;
    import org.springframework.boot.autoconfigure.AutoConfigureOrder;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
    import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import com.olive.route.DatabaseRouteDefinitionRepository;
    import com.olive.router.GatewayRouterService;
    @AutoConfigureBefore(GatewayAutoConfiguration.class)
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 5)
    @Configuration
    public class GatewayConfig {
        /**
         * 網關路由配置實現bean
         */
        @Bean
        @ConditionalOnMissingBean(RouteDefinitionRepository.class)
        @ConditionalOnBean(GatewayRouterService.class)
        public DatabaseRouteDefinitionRepository databaseRouteDefinitionRepository(GatewayRouterService gatewayRouterService) {
            return new DatabaseRouteDefinitionRepository(gatewayRouterService);
        }
        
    }
    

    5. 添加 application.yml 配置文件

    需要啟動nacos,然后要配置 nacos 注冊中心地址。

    server:
      port: 8089
    spring:
      application:
        name: olive-gateway
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.255.10:8848
    

    6. 編寫 springboot 啟動引導類

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    @EnableDiscoveryClient
    @SpringBootApplication
    public class GwApplication {
     public static void main(String[] args) {
       SpringApplication.run(GwApplication.class, args);
     }
     
    }
    


    路由動態路由
    本作品采用《CC 協議》,轉載必須注明作者和本文鏈接
    用戶名:加密密碼:密碼最后一次修改日期:兩次密碼的修改時間間隔:密碼有效期:密碼修改到期到的警告天數:密碼過期之后的寬限天數:賬號失效時間:保留。查看下pid所對應的進程文件路徑,
    動態路由的實現方式多種多樣,研究一下基于數據方式的動態路由。的配置自動化資源過濾。
    由于新發布的路由的Cost值更小,成為優選路由,從而導致環路。DeviceD和DeviceE的OSPF 1收到這條LSA后,會分別計算出到10.0.0.1的路由,其出接口為各自設備的interface1,cost值為21,路由優先級為150。
    值得指出的是,即使是IS-IS協議,也要流經協議棧的網絡層,而網絡層同樣有IS-IS路由表,網絡層同樣要依賴于這個IS-IS路由表提供路由服務。不工作在TCP/IP協議架構之上的IS-IS,查IS-IS路由表。路由協議不能沒有路由表,但是路由表可以沒有路由協議。這里進程之間的通信是指進程使用TCP/IP作為通信的橋梁。需要指出的是,路由協議產生的路由表,并不都是可以進入TCP/IP的全局路由表。
    按照不同的分類標準,網關也有很多種。與動態路由不同,靜態路由是固定的,不會改變,即使網絡狀況已經改變或是重新被組態。在不修改本地連接的IP地址及網關情況下,公司電腦需要能夠同時訪問外網和內網服務器。步驟4: 在PC上訪問內網服務器,檢測靜態路由條目是否生效。
    2022年4月20日,Apache發布安全公告,修復了一個 Apache APISIX中的信息泄露漏洞。漏洞編號: CVE-2022-29266,漏洞威脅等級:嚴重。
    安服工程師技能手冊詳細總結
    近日,國家信息安全漏洞庫(CNNVD)收到關于Apache Apisix 授權問題漏洞(CNNVD-202112-2629、CVE-2021-45232)情況的報送。成功利用漏洞的攻擊者,可以在未經授權的情況下獲取或更改設備的配置信息,進而構造惡意數據對目標設備進行攻擊。Apache APISIX Dashboard 2.10及其之前版本均受此漏洞影響。目前,Apache官方已經發布了版本更新修復
    建議用戶盡快采取修補措施。
    一顆小胡椒
    暫無描述
      亚洲 欧美 自拍 唯美 另类