From 228cf04259e99ffac69ea0c2049c393118095588 Mon Sep 17 00:00:00 2001 From: yunlongn Date: Mon, 14 Oct 2024 11:09:02 +0800 Subject: [PATCH] [type:refactor] refactor springcloud plugin. (#5695) * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. * [type:refactor] refactor springcloud plugin. --------- Co-authored-by: aias00 --- db/init/mysql/schema.sql | 8 +- db/init/ob/schema.sql | 8 +- db/init/og/create-table.sql | 6 +- db/init/oracle/schema.sql | 6 +- db/init/pg/create-table.sql | 6 +- db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql | 8 +- db/upgrade/2.6.1-upgrade-2.7.0-og.sql | 6 +- db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql | 6 +- db/upgrade/2.6.1-upgrade-2.7.0-pg.sql | 6 +- .../main/resources/sql-script/h2/schema.sql | 8 +- .../shenyu-integrated-test-sdk-http/pom.xml | 12 --- .../springcloud/SpringCloudPluginTest.java | 3 +- .../SpringCloudPluginUploadTest.java | 3 +- .../handler/SpringCloudPluginDataHandler.java | 89 +++++++-------- .../SpringCloudHeartBeatListener.java | 11 +- .../ShenyuSpringCloudServiceChooser.java | 19 ++-- .../springcloud/SpringCloudPluginTest.java | 39 +++---- .../SpringCloudPluginDataHandlerTest.java | 2 - .../ShenyuSpringCloudServiceChooserTest.java | 102 +++++++++--------- .../registry/api/config/RegisterConfig.java | 1 - 20 files changed, 164 insertions(+), 185 deletions(-) diff --git a/db/init/mysql/schema.sql b/db/init/mysql/schema.sql index 5ae999949823..0f37731f2f1d 100644 --- a/db/init/mysql/schema.sql +++ b/db/init/mysql/schema.sql @@ -1281,9 +1281,9 @@ INSERT INTO `plugin_handle` VALUES ('1722804548510507021', '14', 'rewriteContext INSERT INTO `plugin_handle` VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO `plugin_handle` VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); -- ---------------------------- -- Table structure for resource @@ -2351,7 +2351,7 @@ INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `c INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822182','649330b6-c2d7-4edc-be8e-8a54df9eb385','45', '{\"host\":\"127.0.0.1\",\"port\":5672,\"password\":\"admin\",\"username\":\"admin\",\"exchangeName\":\"exchange.logging.plugin\",\"queueName\":\"queue.logging.plugin\",\"routingKey\":\"topic.logging\",\"virtualHost\":\"/\",\"exchangeType\":\"direct\",\"durable\":\"true\",\"exclusive\":\"false\",\"autoDelete\":\"false\"}', 171, 0, '2023-11-06 15:49:56.454', '2023-11-10 10:40:58.447'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{\"multiSelectorHandle\":\"1\",\"multiRuleHandle\":\"0\"}', 200, 1, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{\"register\":\"zookeeper://localhost:2181\",\"multiSelectorHandle\":\"1\",\"threadpool\":\"shared\",\"corethreads\":0,\"threads\":2147483647,\"queues\":0}', 310, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); -INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\":{}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\": {}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); diff --git a/db/init/ob/schema.sql b/db/init/ob/schema.sql index 9451421c2c76..73086427e2b3 100644 --- a/db/init/ob/schema.sql +++ b/db/init/ob/schema.sql @@ -1281,9 +1281,9 @@ INSERT INTO `plugin_handle` VALUES ('1722804548510507021', '14', 'rewriteContext INSERT INTO `plugin_handle` VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO `plugin_handle` VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); -- ---------------------------- -- Table structure for resource @@ -2351,7 +2351,7 @@ INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `c INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822182','649330b6-c2d7-4edc-be8e-8a54df9eb385','45', '{\"host\":\"127.0.0.1\",\"port\":5672,\"password\":\"admin\",\"username\":\"admin\",\"exchangeName\":\"exchange.logging.plugin\",\"queueName\":\"queue.logging.plugin\",\"routingKey\":\"topic.logging\",\"virtualHost\":\"/\",\"exchangeType\":\"direct\",\"durable\":\"true\",\"exclusive\":\"false\",\"autoDelete\":\"false\"}', 171, 0, '2023-11-06 15:49:56.454', '2023-11-10 10:40:58.447'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{\"multiSelectorHandle\":\"1\",\"multiRuleHandle\":\"0\"}', 200, 1, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{\"register\":\"zookeeper://localhost:2181\",\"multiSelectorHandle\":\"1\",\"threadpool\":\"shared\",\"corethreads\":0,\"threads\":2147483647,\"queues\":0}', 310, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); -INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\":{}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\": {}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); diff --git a/db/init/og/create-table.sql b/db/init/og/create-table.sql index 6998ecaa67bb..997b87bfeb99 100644 --- a/db/init/og/create-table.sql +++ b/db/init/og/create-table.sql @@ -1357,9 +1357,9 @@ INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507021', '14', 'rewri INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); -- ---------------------------- -- Table structure for resource diff --git a/db/init/oracle/schema.sql b/db/init/oracle/schema.sql index 879169a8752f..646d68da1b1a 100644 --- a/db/init/oracle/schema.sql +++ b/db/init/oracle/schema.sql @@ -2156,13 +2156,13 @@ insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ values ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}'); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL); +values ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL); +values ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL); +values ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(shenyu_dict(type, dict_code, dict_name)) */ into SHENYU_DICT (ID, TYPE, DICT_CODE, DICT_NAME, DICT_VALUE, "desc", SORT, ENABLED) VALUES ('1529402613195784272', 'securityProtocol', 'SECURITY_PROTOCOL', 'PLAINTEXT', 'PLAINTEXT', '', 1, 1); diff --git a/db/init/pg/create-table.sql b/db/init/pg/create-table.sql index e1dcc195ef78..67ffa5cdd3db 100644 --- a/db/init/pg/create-table.sql +++ b/db/init/pg/create-table.sql @@ -1417,9 +1417,9 @@ INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507021', '14', 'rewri INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); -- ---------------------------- -- Table structure for resource diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql b/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql index 912579fadec4..b064c8211c43 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql @@ -19,9 +19,9 @@ INSERT INTO `plugin_handle` VALUES ('1722804548510507021', '14', 'rewriteContext INSERT INTO `plugin_handle` VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO `plugin_handle` VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO `plugin_handle` VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); INSERT INTO `shenyu_dict` VALUES ('1679002911061737478', 'rewriteMetaData', 'REWRITE_META_DATA', 'true', 'true', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO `shenyu_dict` VALUES ('1679002911061737479', 'rewriteMetaData', 'REWRITE_META_DATA', 'false', 'false', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); @@ -142,7 +142,7 @@ INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `c INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822182','649330b6-c2d7-4edc-be8e-8a54df9eb385','45', '{\"host\":\"127.0.0.1\",\"port\":5672,\"password\":\"admin\",\"username\":\"admin\",\"exchangeName\":\"exchange.logging.plugin\",\"queueName\":\"queue.logging.plugin\",\"routingKey\":\"topic.logging\",\"virtualHost\":\"/\",\"exchangeType\":\"direct\",\"durable\":\"true\",\"exclusive\":\"false\",\"autoDelete\":\"false\"}', 171, 0, '2023-11-06 15:49:56.454', '2023-11-10 10:40:58.447'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{\"multiSelectorHandle\":\"1\",\"multiRuleHandle\":\"0\"}', 200, 1, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{\"register\":\"zookeeper://localhost:2181\",\"multiSelectorHandle\":\"1\",\"threadpool\":\"shared\",\"corethreads\":0,\"threads\":2147483647,\"queues\":0}', 310, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); -INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\":{}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{\"enabled\":false,\"registerType\":\"eureka\",\"serverLists\":\"http://localhost:8761/eureka\",\"props\": {}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT INTO `shenyu`.`namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-og.sql b/db/upgrade/2.6.1-upgrade-2.7.0-og.sql index 711ef4e74002..b83fcae76c8f 100644 --- a/db/upgrade/2.6.1-upgrade-2.7.0-og.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-og.sql @@ -18,9 +18,9 @@ INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507020', '14', 'rewriteContextPath', 'rewriteContextPath', 2, 2, 2, '{"required":"0","defaultValue":""}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507021', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507022', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); INSERT INTO "public"."shenyu_dict" VALUES ('1679002911061737478', 'rewriteMetaData', 'REWRITE_META_DATA', 'true', 'true', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."shenyu_dict" VALUES ('1679002911061737479', 'rewriteMetaData', 'REWRITE_META_DATA', 'false', 'false', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql b/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql index 5b58f084978a..e2745c9dfc2f 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql @@ -31,13 +31,13 @@ insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ values ('1722804548510507022', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}'); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL); +values ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL); +values ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL); insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(plugin_handle(plugin_id, field, type)) */ into plugin_handle (ID, PLUGIN_ID, FIELD, LABEL, DATA_TYPE, TYPE, SORT, EXT_OBJ) -values ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL); +values ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL); -- ---------------------------- -- Table structure for SHENYU_LOCK diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql b/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql index 207fabf8cdf0..2371a4267bfa 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql @@ -18,9 +18,9 @@ INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507020', '14', 'rewriteContextPath', 'rewriteContextPath', 2, 2, 2, '{"required":"0","defaultValue":""}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507021', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507022', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}', '2024-02-07 14:31:49', '2024-02-07 14:31:49'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); +INSERT INTO "public"."plugin_handle" VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); INSERT INTO "public"."shenyu_dict" VALUES ('1679002911061737478', 'rewriteMetaData', 'REWRITE_META_DATA', 'true', 'true', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); INSERT INTO "public"."shenyu_dict" VALUES ('1679002911061737479', 'rewriteMetaData', 'REWRITE_META_DATA', 'false', 'false', '', 4, 1, '2024-02-07 14:31:49', '2024-02-07 14:31:49'); diff --git a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql index 4d0d92c00715..ab962951f083 100755 --- a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql +++ b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql @@ -911,9 +911,9 @@ INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`, INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"required":"1","defaultValue":"100"}'); INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('1722804548510507023', '3', 'rewriteMetaData', 'rewriteMetaData', 3, 2, 3, '{"required":"1","defaultValue":"false"}'); -INSERT IGNORE INTO `plugin_handle`(`id`, `plugin_id`, `field`, `label`, `data_type`, `type`, `sort`, `ext_obj`, `date_created`, `date_updated`) VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 3, 3, 1, NULL, '2024-08-24 09:40:03.293', '2024-08-24 21:52:27.920'); -INSERT IGNORE INTO `plugin_handle`(`id`, `plugin_id`, `field`, `label`, `data_type`, `type`, `sort`, `ext_obj`, `date_created`, `date_updated`) VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 3, 3, 2, NULL, '2024-08-24 21:52:51.179', '2024-08-24 21:53:27.483'); -INSERT IGNORE INTO `plugin_handle`(`id`, `plugin_id`, `field`, `label`, `data_type`, `type`, `sort`, `ext_obj`, `date_created`, `date_updated`) VALUES ('1722804548510507026', '8', 'props', 'props', 3, 3, 3, NULL, '2024-08-24 21:53:25.764', '2024-08-24 21:53:30.255'); +INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('1722804548510507024', '8', 'registerType', 'registerType', 2, 3, 1, NULL); +INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('1722804548510507025', '8', 'serverLists', 'serverLists', 2, 3, 2, NULL); +INSERT IGNORE INTO plugin_handle (`id`, `plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`) VALUES ('1722804548510507026', '8', 'props', 'props', 4, 3, 3, NULL); /** insert resource for resource */ INSERT IGNORE INTO `resource` (`id`, `parent_id`, `title`, `name`, `url`, `component`, `resource_type`, `sort`, `icon`, `is_leaf`, `is_route`, `perms`, `status`) VALUES('1346775491550474240','','SHENYU.MENU.PLUGIN.LIST','plug','/plug','PluginList','0','0','dashboard','0','0','','1'); @@ -1318,7 +1318,7 @@ INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `con INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822182','649330b6-c2d7-4edc-be8e-8a54df9eb385','45', '{"host":"127.0.0.1","port":5672,"password":"admin","username":"admin","exchangeName":"exchange.logging.plugin","queueName":"queue.logging.plugin","routingKey":"topic.logging","virtualHost":"/","exchangeType":"direct","durable":"true","exclusive":"false","autoDelete":"false"}', 171, 0, '2023-11-06 15:49:56.454', '2023-11-10 10:40:58.447'); INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{"multiSelectorHandle":"1","multiRuleHandle":"0"}', 200, 1, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{"register":"zookeeper://localhost:2181","multiSelectorHandle":"1","threadpool":"shared","corethreads":0,"threads":2147483647,"queues":0}', 310, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); -INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{"enabled":false,"registerType":"eureka","serverLists":"http://localhost:8761/eureka","props":{}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{"enabled":false,"registerType":"eureka","serverLists":"http://localhost:8761/eureka","props": {}}', 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); INSERT IGNORE INTO `namespace_plugin_rel` (`id`,`namespace_id`,`plugin_id`, `config`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml b/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml index a70ef0def1bc..f8e0159b3049 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml +++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml @@ -46,18 +46,6 @@ - - - - - - - - - - - - diff --git a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java index 17cb61bf5e5f..2f130ffedbe2 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java +++ b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java @@ -34,7 +34,8 @@ public class SpringCloudPluginTest extends AbstractPluginDataInit { @BeforeAll public static void setup() throws IOException { - String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), ""); + String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), + "{\"enabled\":true,\"registerType\":\"eureka\",\"serverLists\":\"http://shenyu-examples-eureka:8761/eureka\"}"); assertThat(pluginResult, is("success")); } diff --git a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java index a7e40cc728b1..034db7d70ab0 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java +++ b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginUploadTest.java @@ -48,7 +48,8 @@ public class SpringCloudPluginUploadTest extends AbstractPluginDataInit { @BeforeAll public static void setup() throws IOException { - String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), ""); + String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), + "{\"enabled\":true,\"registerType\":\"eureka\",\"serverLists\":\"http://shenyu-examples-eureka:8761/eureka\"}"); assertThat(pluginResult, is("success")); Path filePath = Paths.get(FILE_PATH); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandler.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandler.java index 45d1185f8454..e8cb2f27985b 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandler.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandler.java @@ -17,9 +17,11 @@ package org.apache.shenyu.plugin.springcloud.handler; -import org.apache.commons.collections4.CollectionUtils; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.client.naming.utils.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; +import org.apache.shenyu.common.config.ShenyuConfig; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.PluginData; import org.apache.shenyu.common.dto.RuleData; @@ -47,6 +49,7 @@ import java.util.List; import java.util.Optional; +import java.util.Properties; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -63,51 +66,23 @@ public class SpringCloudPluginDataHandler implements PluginDataHandler { private static final Logger LOG = LoggerFactory.getLogger(SpringCloudPluginDataHandler.class); - private final SpringCloudCacheConfig springCloudCacheConfig; + private final ShenyuConfig.SpringCloudCacheConfig springCloudCacheConfig; private final Environment env; - public SpringCloudPluginDataHandler(final SpringCloudCacheConfig springCloudCacheConfig, final Environment env) { + public SpringCloudPluginDataHandler(final ShenyuConfig.SpringCloudCacheConfig springCloudCacheConfig, final Environment env) { this.springCloudCacheConfig = springCloudCacheConfig; this.env = env; } - private void reNewAndInitShenyuInstanceRegisterRepository(final RegisterConfig refreshRegisterConfig) { - if (refreshRegisterConfig == null) { - return; - } - LOG.info("springCloud handlerPlugin refreshRegisterConfig = {}", GsonUtils.getInstance().toJson(refreshRegisterConfig)); - repository = ShenyuInstanceRegisterRepositoryFactory.reNewAndInitInstance(refreshRegisterConfig); - LOG.info("springCloud handlerPlugin repository = {}", repository); - } - - private void reNewAndInitShenyuInstanceRegisterRepositoryByYml() { - boolean enable = Boolean.parseBoolean(env.getProperty("eureka.client.enabled")); - String serverLists = env.getProperty("eureka.client.serviceUrl.defaultZone"); - if (enable) { - RegisterConfig.Builder builder = RegisterConfig.Builder.builder().enabled(enable).registerType("eureka").serverLists(serverLists); - reNewAndInitShenyuInstanceRegisterRepository(builder.build()); - return; - } - - enable = Boolean.parseBoolean(env.getProperty("spring.cloud.nacos.discovery.enabled")); - serverLists = env.getProperty("spring.cloud.nacos.discovery.server-addr"); - if (enable) { - RegisterConfig.Builder builder = RegisterConfig.Builder.builder().enabled(enable).registerType("eureka").serverLists(serverLists); - reNewAndInitShenyuInstanceRegisterRepository(builder.build()); - return; - } - } - @Override public void handlerPlugin(final PluginData pluginData) { - LOG.info("pluginData = {}", GsonUtils.getInstance().toJson(pluginData)); - if (pluginData == null || StringUtils.isBlank(pluginData.getConfig())) { - // consider yml config, as eureka or nacos - reNewAndInitShenyuInstanceRegisterRepositoryByYml(); + if (!pluginData.getEnabled()) { return; } - if (!pluginData.getEnabled()) { + if (StringUtils.isBlank(pluginData.getConfig())) { + // consider yml config, as eureka or nacos + this.readYmlBuildRepository(); return; } // get old pluginData @@ -118,26 +93,22 @@ public void handlerPlugin(final PluginData pluginData) { if (newRegisterConfig == null) { return; } - if (oldPluginData != null) { - LOG.info("oldPluginData = {}", GsonUtils.getInstance().toJson(oldPluginData)); - } RegisterConfig oldRegisterConfig = null; if (StringUtils.isNotBlank(oldConfig)) { oldRegisterConfig = GsonUtils.getInstance().fromJson(oldConfig, RegisterConfig.class); } // refresh config - RegisterConfig refreshRegisterConfig = GsonUtils.getInstance().fromJson(newConfig, RegisterConfig.class); if (repository == null) { LOG.info("springCloud handlerPlugin repository is null"); - reNewAndInitShenyuInstanceRegisterRepository(refreshRegisterConfig); + repository = ShenyuInstanceRegisterRepositoryFactory.reNewAndInitInstance(newRegisterConfig); } else if (!newRegisterConfig.equals(oldRegisterConfig)) { LOG.info("springCloud handlerPlugin repository occur update"); // the config has been updated if (repository != null) { repository.close(); } - reNewAndInitShenyuInstanceRegisterRepository(refreshRegisterConfig); + repository = ShenyuInstanceRegisterRepositoryFactory.reNewAndInitInstance(newRegisterConfig); } } @@ -202,4 +173,38 @@ private List convertUpstreamList(final List upstreamLi public static ShenyuInstanceRegisterRepository getRepository() { return repository; } + + private void readYmlBuildRepository() { + // enable cloud discovery + if (!Boolean.parseBoolean(env.getProperty("spring.cloud.discovery.enabled"))) { + return; + } + + if (Boolean.parseBoolean(env.getProperty("eureka.client.enabled"))) { + RegisterConfig registerConfig = RegisterConfig.Builder.builder() + .enabled(true) + .registerType("eureka") + .serverLists(env.getProperty("eureka.client.serviceUrl.defaultZone")).build(); + repository = ShenyuInstanceRegisterRepositoryFactory.reNewAndInitInstance(registerConfig); + return; + } + if (Boolean.parseBoolean(env.getProperty("spring.cloud.nacos.discovery.enabled"))) { + final String serverLists = env.getProperty("spring.cloud.nacos.discovery.server-addr"); + final String prefix = "spring.cloud.nacos.discovery."; + Properties properties = new Properties(); + properties.put(PropertyKeyConst.NAMESPACE, env.getProperty(prefix + PropertyKeyConst.NAMESPACE)); + properties.put(PropertyKeyConst.USERNAME, env.getProperty(prefix + PropertyKeyConst.USERNAME)); + properties.put(PropertyKeyConst.PASSWORD, env.getProperty(prefix + PropertyKeyConst.PASSWORD)); + properties.put(PropertyKeyConst.ACCESS_KEY, env.getProperty(prefix + PropertyKeyConst.ACCESS_KEY)); + properties.put(PropertyKeyConst.SECRET_KEY, env.getProperty(prefix + PropertyKeyConst.SECRET_KEY)); + RegisterConfig registerConfig = RegisterConfig.Builder.builder() + .enabled(true) + .registerType("nacos") + .serverLists(serverLists) + .props(properties) + .build(); + repository = ShenyuInstanceRegisterRepositoryFactory.reNewAndInitInstance(registerConfig); + } + } + } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/listener/SpringCloudHeartBeatListener.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/listener/SpringCloudHeartBeatListener.java index cd47c8b71074..0e6fb684c35a 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/listener/SpringCloudHeartBeatListener.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/listener/SpringCloudHeartBeatListener.java @@ -17,13 +17,12 @@ package org.apache.shenyu.plugin.springcloud.listener; + import org.apache.commons.collections4.MapUtils; -import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; +import org.apache.shenyu.common.config.ShenyuConfig; import org.apache.shenyu.common.dto.convert.selector.SpringCloudSelectorHandle; import org.apache.shenyu.common.utils.LogUtils; import org.apache.shenyu.plugin.springcloud.cache.ServiceInstanceCache; -import static org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler.SELECTOR_CACHED; - import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; import org.apache.shenyu.registry.api.entity.InstanceEntity; import org.slf4j.Logger; @@ -34,6 +33,8 @@ import java.util.List; import java.util.Map; +import static org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler.SELECTOR_CACHED; + /** * SpringCloud HeartBeat Listener. */ @@ -41,9 +42,9 @@ public class SpringCloudHeartBeatListener implements ApplicationListener upstreamList, final String loadba * @return {@linkplain ServiceInstance} */ private List getServiceInstance(final String serviceId) { - if (CollectionUtils.isEmpty(ServiceInstanceCache.getServiceInstance(serviceId))) { - List instances = null; - if (SpringCloudPluginDataHandler.getRepository() != null) { - instances = SpringCloudPluginDataHandler.getRepository().selectInstances(serviceId); - } - LOG.info("getServiceInstance: {}", JsonUtils.toJson(instances)); - return Optional.ofNullable(instances).orElse(Collections.emptyList()); + if (CollectionUtils.isNotEmpty(ServiceInstanceCache.getServiceInstance(serviceId))) { + return ServiceInstanceCache.getServiceInstance(serviceId); } - LOG.info("ServiceInstanceCache.getServiceInstance(serviceId)"); - return ServiceInstanceCache.getServiceInstance(serviceId); + List instances = null; + if (SpringCloudPluginDataHandler.getRepository() != null) { + instances = SpringCloudPluginDataHandler.getRepository().selectInstances(serviceId); + } + final List instanceEntities = Optional.ofNullable(instances).orElse(Collections.emptyList()); + LOG.info("ShenyuSpringCloudServiceChooser selectInstance size: {}", instanceEntities.size()); + return instanceEntities; } /** diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/SpringCloudPluginTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/SpringCloudPluginTest.java index 96634e4a6c0c..c5afa0df64b9 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/SpringCloudPluginTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/SpringCloudPluginTest.java @@ -17,28 +17,18 @@ package org.apache.shenyu.plugin.springcloud; -import org.apache.shenyu.common.dto.SelectorData; -import org.apache.shenyu.plugin.api.ShenyuPluginChain; -import org.apache.shenyu.plugin.api.context.ShenyuContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; -import org.springframework.web.server.ServerWebExchange; -import java.net.URISyntaxException; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import com.google.common.collect.Lists; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.RuleData; +import org.apache.shenyu.common.dto.SelectorData; import org.apache.shenyu.common.dto.convert.rule.impl.SpringCloudRuleHandle; import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; import org.apache.shenyu.common.dto.convert.selector.SpringCloudSelectorHandle; import org.apache.shenyu.common.enums.PluginEnum; import org.apache.shenyu.common.enums.RpcTypeEnum; import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.plugin.api.ShenyuPluginChain; +import org.apache.shenyu.plugin.api.context.ShenyuContext; import org.apache.shenyu.plugin.api.result.DefaultShenyuResult; import org.apache.shenyu.plugin.api.result.ShenyuResult; import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; @@ -47,22 +37,32 @@ import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; import org.apache.shenyu.plugin.springcloud.loadbalance.ShenyuSpringCloudServiceChooser; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.HttpMethod; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import java.net.InetSocketAddress; +import java.net.URISyntaxException; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * The Test Case For {@link SpringCloudPlugin}. @@ -97,19 +97,6 @@ public void setUp() { chain = mock(ShenyuPluginChain.class); selector = SelectorData.builder().id("1").enabled(true).build(); -// final List serviceInstanceList = new ArrayList<>(); -// DefaultServiceInstance defaultServiceInstance = new DefaultServiceInstance(); -// defaultServiceInstance.setServiceId("serviceId"); -// defaultServiceInstance.setUri(URI.create("http://localhost:8080")); -// defaultServiceInstance.setInstanceId("serviceId"); -// defaultServiceInstance.setPort(8080); -// defaultServiceInstance.setHost("localhost"); -// serviceInstanceList.add(defaultServiceInstance); -// SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); -// Map> serviceInstanceMap = new HashMap<>(); -// serviceInstanceMap.put(defaultServiceInstance.getInstanceId(), serviceInstanceList); -// simpleDiscoveryProperties.setInstances(serviceInstanceMap); -// SimpleDiscoveryClient discoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); ShenyuSpringCloudServiceChooser loadBalancerClient = mock(ShenyuSpringCloudServiceChooser.class); springCloudPlugin = new SpringCloudPlugin(loadBalancerClient); } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandlerTest.java index 7d77708442bf..6a254d91fbcb 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandlerTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/handler/SpringCloudPluginDataHandlerTest.java @@ -58,8 +58,6 @@ public final class SpringCloudPluginDataHandlerTest { @BeforeEach public void setUp() { this.mockSpringCloudConfig(); -// SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); -// SimpleDiscoveryClient discoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); ShenyuConfig.SpringCloudCacheConfig springCloudCacheConfig = SpringBeanUtils.getInstance().getBean(SpringCloudCacheConfig.class); ConfigurableApplicationContext context = (ConfigurableApplicationContext) SpringBeanUtils.getInstance().getApplicationContext(); this.springCloudPluginDataHandler = new SpringCloudPluginDataHandler(springCloudCacheConfig, context.getEnvironment()); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooserTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooserTest.java index 250529cde4ee..cb755593835a 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooserTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/test/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooserTest.java @@ -18,16 +18,6 @@ package org.apache.shenyu.plugin.springcloud.loadbalance; import org.apache.shenyu.common.config.ShenyuConfig; -import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; -import org.springframework.context.ConfigurableApplicationContext; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; import org.apache.shenyu.common.dto.SelectorData; import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; @@ -35,11 +25,28 @@ import org.apache.shenyu.common.utils.GsonUtils; import org.apache.shenyu.loadbalancer.entity.Upstream; import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; +import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.registry.api.entity.InstanceEntity; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.springframework.context.ConfigurableApplicationContext; +import java.net.URI; import java.util.ArrayList; import java.util.List; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + /** * The Test Case For ShenyuSpringCloudLoadBalancerClientTest. */ @@ -51,6 +58,8 @@ public class ShenyuSpringCloudServiceChooserTest { private SpringCloudPluginDataHandler springCloudPluginDataHandler; + private ShenyuInstanceRegisterRepository shenyuInstanceRegisterRepository; + @BeforeEach public void setup() { this.mockSpringCloudConfig(); @@ -58,6 +67,8 @@ public void setup() { ConfigurableApplicationContext context = (ConfigurableApplicationContext) SpringBeanUtils.getInstance().getApplicationContext(); springCloudPluginDataHandler = new SpringCloudPluginDataHandler(springCloudCacheConfig, context.getEnvironment()); + + shenyuInstanceRegisterRepository = mock(ShenyuInstanceRegisterRepository.class); } @Test @@ -114,55 +125,44 @@ public void testChoose() { @Test public void testLoadBalancer() { -// final List serviceInstances = new ArrayList<>(); -// DefaultServiceInstance defaultServiceInstance = new DefaultServiceInstance(); -// defaultServiceInstance.setServiceId("serviceId"); -// defaultServiceInstance.setUri(URI.create("http://localhost:8081")); -// defaultServiceInstance.setInstanceId("serviceId"); -// defaultServiceInstance.setPort(8081); -// defaultServiceInstance.setHost("localhost"); -// -// DefaultServiceInstance defaultServiceInstance2 = new DefaultServiceInstance(); -// defaultServiceInstance2.setServiceId("serviceId"); -// defaultServiceInstance2.setUri(URI.create("http://localhost:8080")); -// defaultServiceInstance2.setInstanceId("serviceId"); -// defaultServiceInstance2.setPort(8080); -// defaultServiceInstance2.setHost("localhost"); -// serviceInstances.add(defaultServiceInstance); -// serviceInstances.add(defaultServiceInstance2); -// -// SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); -// Map> serviceInstanceMap = new HashMap<>(); -// serviceInstanceMap.put(defaultServiceInstance.getInstanceId(), serviceInstances); -// simpleDiscoveryProperties.setInstances(serviceInstanceMap); -// final SimpleDiscoveryClient simpleDiscoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); -// final ShenyuSpringCloudServiceChooser shenyuServiceChoose = new ShenyuSpringCloudServiceChooser(simpleDiscoveryClient); - final String ip = "0.0.0.0"; final String selectorId = "1"; final String loadbalancer = "roundRobin"; -// final SpringCloudSelectorHandle springCloudSelectorHandle = SpringCloudSelectorHandle.builder() -// .serviceId("serviceId") -// .gray(false) -// .build(); -// final SelectorData selectorData = SelectorData.builder() -// .handle(GsonUtils.getInstance().toJson(springCloudSelectorHandle)) -// .id("1") -// .build(); -// springCloudPluginDataHandler.handlerSelector(selectorData); + final SpringCloudSelectorHandle springCloudSelectorHandle = SpringCloudSelectorHandle.builder() + .serviceId("serviceId") + .gray(false) + .build(); + final SelectorData selectorData = SelectorData.builder() + .handle(GsonUtils.getInstance().toJson(springCloudSelectorHandle)) + .id("1") + .build(); + springCloudPluginDataHandler.handlerSelector(selectorData); - // mock data - Upstream mockUpstream1 = Upstream.builder().url("localhost:8081").protocol("http").build(); - Upstream mockUpstream2 = Upstream.builder().url("localhost:8080").protocol("http").build(); + final List serviceInstances = new ArrayList<>(); + InstanceEntity defaultServiceInstance = new InstanceEntity(); + defaultServiceInstance.setUri(URI.create("http://localhost:8081")); + defaultServiceInstance.setPort(8081); + defaultServiceInstance.setHost("localhost"); + + InstanceEntity defaultServiceInstance2 = new InstanceEntity(); + defaultServiceInstance2.setUri(URI.create("http://localhost:8080")); + defaultServiceInstance2.setPort(8080); + defaultServiceInstance2.setHost("localhost"); + serviceInstances.add(defaultServiceInstance); + serviceInstances.add(defaultServiceInstance2); + final ShenyuSpringCloudServiceChooser shenyuServiceChoose = new ShenyuSpringCloudServiceChooser(); + + final MockedStatic springCloudPluginDataHandlerMockedStatic = mockStatic(SpringCloudPluginDataHandler.class); + springCloudPluginDataHandlerMockedStatic.when(SpringCloudPluginDataHandler::getRepository).thenReturn(shenyuInstanceRegisterRepository); + // mock return value + when(shenyuInstanceRegisterRepository.selectInstances(any())).thenReturn(serviceInstances); - when(serviceChooser.choose("serviceId", selectorId, ip, loadbalancer)).thenReturn(mockUpstream1); - Upstream upstream1 = serviceChooser.choose("serviceId", selectorId, ip, loadbalancer); - when(serviceChooser.choose("serviceId", selectorId, ip, loadbalancer)).thenReturn(mockUpstream2); - Upstream upstream2 = serviceChooser.choose("serviceId", selectorId, ip, loadbalancer); + Upstream upstream1 = shenyuServiceChoose.choose("serviceId", selectorId, ip, loadbalancer); + Upstream upstream2 = shenyuServiceChoose.choose("serviceId", selectorId, ip, loadbalancer); // if roundRobin, upstream1 not equals upstream2 Assertions.assertNotEquals(upstream1, upstream2); } - + private void mockSpringCloudConfig() { ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class); // create mock object diff --git a/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/config/RegisterConfig.java b/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/config/RegisterConfig.java index 59886aa3ce05..3a8e5d507e40 100644 --- a/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/config/RegisterConfig.java +++ b/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/config/RegisterConfig.java @@ -161,7 +161,6 @@ public int hashCode() { int result = getRegisterType() != null ? getRegisterType().hashCode() : 0; result = 31 * result + (getServerLists() != null ? getServerLists().hashCode() : 0); - // 对 Props 进行处理 if (getProps() != null) { for (Map.Entry entry : getProps().entrySet()) { result = 31 * result + (entry.getKey() != null ? entry.getKey().hashCode() : 0);