This is an automated email from the ASF dual-hosted git repository. yunlong pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 5480f87608 springcloud dynamic register (#5676) 5480f87608 is described below commit 5480f87608ff1e405a70ad43f99cc6c84d770e0b Author: WindSearcher <windsearc...@qq.com> AuthorDate: Thu Oct 10 20:01:46 2024 +0800 springcloud dynamic register (#5676) * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register * dynamic register --------- Co-authored-by: aias00 <rok...@163.com> Co-authored-by: yunlongn <yunlo...@outlook.com> --- db/init/mysql/schema.sql | 6 +- db/init/ob/schema.sql | 6 +- db/init/og/create-table.sql | 6 +- db/init/oracle/schema.sql | 11 +- db/init/pg/create-table.sql | 6 +- db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql | 6 +- db/upgrade/2.6.1-upgrade-2.7.0-og.sql | 5 +- db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql | 11 +- db/upgrade/2.6.1-upgrade-2.7.0-pg.sql | 5 +- .../src/main/resources/sql-script/h2/schema.sql | 6 +- .../springcloud/SpringCloudPluginTest.java | 1 + .../shenyu-examples-sdk-http/pom.xml | 6 + .../shenyu-integrated-test-sdk-http/pom.xml | 18 +++ .../src/main/resources/application-local.yml | 8 ++ .../shenyu/plugin/api/utils/SpringBeanUtils.java | 4 + .../base/cache/CommonPluginDataSubscriber.java | 2 +- .../shenyu-plugin-springcloud/pom.xml | 5 + .../springcloud/cache/ServiceInstanceCache.java | 7 +- .../handler/SpringCloudPluginDataHandler.java | 103 ++++++++++++++-- .../listener/SpringCloudHeartBeatListener.java | 12 +- .../ShenyuSpringCloudServiceChooser.java | 20 +-- .../plugin/springcloud/SpringCloudPluginTest.java | 76 ++++++------ .../handler/SpringCloudPluginDataHandlerTest.java | 24 ++-- .../ShenyuSpringCloudServiceChooserTest.java | 134 +++++++++++---------- .../shenyu/registry/api/config/RegisterConfig.java | 127 +++++++++++++++++++ .../shenyu/registry/api/entity/InstanceEntity.java | 35 ++++++ .../apollo/ApolloInstanceRegisterRepository.java | 13 +- .../consul/ConsulInstanceRegisterRepository.java | 17 +++ shenyu-registry/shenyu-registry-core/pom.xml | 5 + .../ShenyuInstanceRegisterRepositoryFactory.java | 13 ++ .../etcd/EtcdInstanceRegisterRepository.java | 13 +- shenyu-registry/shenyu-registry-eureka/pom.xml | 22 +++- .../eureka/EurekaInstanceRegisterRepository.java | 66 ++++++---- .../nacos/NacosInstanceRegisterRepository.java | 19 ++- .../polaris/PolarisInstanceRegisterRepository.java | 3 + .../ZookeeperInstanceRegisterRepository.java | 13 +- .../SpringCloudPluginConfiguration.java | 21 ++-- 37 files changed, 656 insertions(+), 199 deletions(-) diff --git a/db/init/mysql/schema.sql b/db/init/mysql/schema.sql index 947b9044ba..09bf1a427f 100644 --- a/db/init/mysql/schema.sql +++ b/db/init/mysql/schema.sql @@ -1281,6 +1281,10 @@ 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'); + -- ---------------------------- -- Table structure for resource -- ---------------------------- @@ -2347,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\",\"exc [...] 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', NULL, 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 34e1ec8557..b3f4cd35ab 100644 --- a/db/init/ob/schema.sql +++ b/db/init/ob/schema.sql @@ -1281,6 +1281,10 @@ 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'); + -- ---------------------------- -- Table structure for resource -- ---------------------------- @@ -2347,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\",\"exc [...] 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', NULL, 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 4dff67a8ff..2577c6a875 100644 --- a/db/init/og/create-table.sql +++ b/db/init/og/create-table.sql @@ -1357,6 +1357,10 @@ 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'); + -- ---------------------------- -- Table structure for resource -- ---------------------------- @@ -2649,7 +2653,7 @@ INSERT INTO "public"."namespace_plugin_rel" VALUES ('1801816010882822181','64933 INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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/oracle/schema.sql b/db/init/oracle/schema.sql index b809b95a6c..0544837c1f 100644 --- a/db/init/oracle/schema.sql +++ b/db/init/oracle/schema.sql @@ -2155,6 +2155,15 @@ values ('1722804548510507022', '14', 'percentage', 'percentage', 1, 2, 3, '{"req 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 ('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); + +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); + +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); + 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); @@ -2834,7 +2843,7 @@ INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespa INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) 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":"fal [...] INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{"multiSelectorHandle":"1","multiRuleHandle":"0"}', 200, 1); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{"register":"zookeeper://localhost:2181","multiSelectorHandle":"1","threadpool":"shared","corethreads":0,"threads":2147483647,"queues":0}', 310, 0); -INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0); +INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{"enabled":false,"registerType":"eureka","serverLists":"http://localhost:8761/eureka","props":{}}', 200, 0); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0); diff --git a/db/init/pg/create-table.sql b/db/init/pg/create-table.sql index 93ae067c3a..bdf7c40e67 100644 --- a/db/init/pg/create-table.sql +++ b/db/init/pg/create-table.sql @@ -1417,6 +1417,10 @@ 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'); + -- ---------------------------- -- Table structure for resource -- ---------------------------- @@ -2768,7 +2772,7 @@ INSERT INTO "public"."namespace_plugin_rel" VALUES ('1801816010882822181','64933 INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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-mysql.sql b/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql index 02991f1c3e..c17cb70ad9 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,6 +19,10 @@ 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 `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'); @@ -138,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\",\"exc [...] 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', NULL, 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 e41751b3db..c192eab6a0 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,6 +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"."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'); @@ -165,7 +168,7 @@ INSERT INTO "public"."namespace_plugin_rel" VALUES ('1801816010882822181','64933 INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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-oracle.sql b/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql index c2d5be43f0..fe3d0a4116 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 @@ -30,6 +30,15 @@ values ('1722804548510507021', '14', 'percentage', 'percentage', 1, 2, 3, '{"req 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 ('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); + +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); + +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); + -- ---------------------------- -- Table structure for SHENYU_LOCK -- ---------------------------- @@ -191,7 +200,7 @@ INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespa INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) 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":"fal [...] INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822183','649330b6-c2d7-4edc-be8e-8a54df9eb385','5', '{"multiSelectorHandle":"1","multiRuleHandle":"0"}', 200, 1); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822184','649330b6-c2d7-4edc-be8e-8a54df9eb385','6', '{"register":"zookeeper://localhost:2181","multiSelectorHandle":"1","threadpool":"shared","corethreads":0,"threads":2147483647,"queues":0}', 310, 0); -INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0); +INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', '{"enabled":false,"registerType":"eureka","serverLists":"http://localhost:8761/eureka","props":{}}', 200, 0); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (namespace_plugin_rel(id)) */ INTO namespace_plugin_rel (id,namespace_id,plugin_id, config, sort, enabled) VALUES ('1801816010882822186','649330b6-c2d7-4edc-be8e-8a54df9eb385','9', NULL, 130, 0); 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 74c7b68a2e..3db0a0a23d 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,6 +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"."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'); @@ -164,7 +167,7 @@ INSERT INTO "public"."namespace_plugin_rel" VALUES ('1801816010882822181','64933 INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" VALUES ('1801816010882822185','649330b6-c2d7-4edc-be8e-8a54df9eb385','8', NULL, 200, 0, '2022-05-25 18:02:53.000', '2022-05-25 18:02:53.000'); +INSERT INTO "public"."namespace_plugin_rel" 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 "public"."namespace_plugin_rel" 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-admin/src/main/resources/sql-script/h2/schema.sql b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql index c4f223e69f..5566360ef4 100755 --- a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql +++ b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql @@ -911,6 +911,10 @@ 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 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'); @@ -1314,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"}', 1 [...] 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', NULL, 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-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java index 605a4fee25..2bec9194fb 100644 --- a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java +++ b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java @@ -81,6 +81,7 @@ public class SpringCloudPluginTest { reqBody.put("enabled", "true"); reqBody.put("role", "Proxy"); reqBody.put("sort", "200"); + reqBody.put("config", "{\"enabled\":true,\"registerType\":\"eureka\",\"serverLists\":\"http://shenyu-examples-eureka:8761/eureka\"}"); reqBody.put("namespaceId", Constants.SYS_DEFAULT_NAMESPACE_NAMESPACE_ID); adminClient.changePluginStatus("1801816010882822185", reqBody); WaitDataSync.waitGatewayPluginUse(gatewayClient, "org.apache.shenyu.plugin.springcloud.SpringCloudPlugin"); diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-http/pom.xml b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-http/pom.xml index a727f09224..2e01bd5c6b 100644 --- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-http/pom.xml +++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-http/pom.xml @@ -31,6 +31,12 @@ <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-sdk-core</artifactId> + <exclusions> + <exclusion> + <groupId>org.apache.shenyu</groupId> + <artifactId>shenyu-registry-eureka</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> 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 359d5e378d..a70ef0def1 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml +++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-http/pom.xml @@ -38,8 +38,26 @@ <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-registry</artifactId> <version>${project.version}</version> + <exclusions> + <exclusion> + <groupId>org.apache.shenyu</groupId> + <artifactId>shenyu-registry-eureka</artifactId> + </exclusion> + </exclusions> </dependency> +<!-- <dependency>--> +<!-- <groupId>org.springframework.cloud</groupId>--> +<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>--> +<!-- <version>4.1.2</version>--> +<!-- <exclusions>--> +<!-- <exclusion>--> +<!-- <groupId>org.springframework.cloud</groupId>--> +<!-- <artifactId>spring-cloud-starter-loadbalancer</artifactId>--> +<!-- </exclusion>--> +<!-- </exclusions>--> +<!-- </dependency>--> + </dependencies> <profiles> diff --git a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml index d7fe2ffbbf..446f628cdc 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml +++ b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/main/resources/application-local.yml @@ -27,6 +27,14 @@ spring: # discovery: # server-addr: 127.0.0.1:8848 +eureka: + client: + enabled: true + serviceUrl: + defaultZone: http://shenyu-examples-eureka:8761/eureka/ + instance: + prefer-ip-address: true + management: health: defaults: diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/SpringBeanUtils.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/SpringBeanUtils.java index 4caa851a29..37f1d21a80 100644 --- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/SpringBeanUtils.java +++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/SpringBeanUtils.java @@ -150,4 +150,8 @@ public final class SpringBeanUtils { public void setApplicationContext(final ApplicationContext applicationContext) { this.applicationContext = applicationContext; } + + public ApplicationContext getApplicationContext() { + return applicationContext; + } } diff --git a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonPluginDataSubscriber.java b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonPluginDataSubscriber.java index 927747c00c..e42f4f2844 100644 --- a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonPluginDataSubscriber.java +++ b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonPluginDataSubscriber.java @@ -214,10 +214,10 @@ public class CommonPluginDataSubscriber implements PluginDataSubscriber { if (data instanceof PluginData) { PluginData pluginData = (PluginData) data; final PluginData oldPluginData = BaseDataCache.getInstance().obtainPluginData(pluginData.getName()); - BaseDataCache.getInstance().cachePluginData(pluginData); Optional.ofNullable(handlerMap.get(pluginData.getName())) .ifPresent(handler -> handler.handlerPlugin(pluginData)); + BaseDataCache.getInstance().cachePluginData(pluginData); // update enabled plugins PluginHandlerEventEnum state = Boolean.TRUE.equals(pluginData.getEnabled()) ? PluginHandlerEventEnum.ENABLED : PluginHandlerEventEnum.DISABLED; diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/pom.xml b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/pom.xml index 7326b329c2..38ec0d10f3 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/pom.xml +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/pom.xml @@ -48,5 +48,10 @@ <artifactId>shenyu-loadbalancer</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.shenyu</groupId> + <artifactId>shenyu-registry-core</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project> diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/cache/ServiceInstanceCache.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/cache/ServiceInstanceCache.java index 4fb502e331..653fe94c35 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/cache/ServiceInstanceCache.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/cache/ServiceInstanceCache.java @@ -19,6 +19,7 @@ package org.apache.shenyu.plugin.springcloud.cache; import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.registry.api.entity.InstanceEntity; import org.springframework.cloud.client.ServiceInstance; import java.util.Collections; @@ -31,7 +32,7 @@ import java.util.Optional; */ public class ServiceInstanceCache { - private static final Map<String, List<ServiceInstance>> SERVICE_INSTANCE_MAP = Maps.newConcurrentMap(); + private static final Map<String, List<InstanceEntity>> SERVICE_INSTANCE_MAP = Maps.newConcurrentMap(); /** * Cache service instance. @@ -39,7 +40,7 @@ public class ServiceInstanceCache { * @param serviceId service id * @param serviceInstances service instance list */ - public static void cacheServiceInstance(final String serviceId, final List<ServiceInstance> serviceInstances) { + public static void cacheServiceInstance(final String serviceId, final List<InstanceEntity> serviceInstances) { if (StringUtils.isNotBlank(serviceId)) { SERVICE_INSTANCE_MAP.put(serviceId, Optional.ofNullable(serviceInstances).orElse(Collections.emptyList())); } @@ -50,7 +51,7 @@ public class ServiceInstanceCache { * @param serviceId service id * @return the list of {@linkplain ServiceInstance} */ - public static List<ServiceInstance> getServiceInstance(final String serviceId) { + public static List<InstanceEntity> getServiceInstance(final String serviceId) { if (StringUtils.isBlank(serviceId)) { return Collections.emptyList(); } 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 f2cd69e3e8..45d1185f84 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 @@ -18,8 +18,10 @@ package org.apache.shenyu.plugin.springcloud.handler; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.common.dto.PluginData; import org.apache.shenyu.common.dto.RuleData; import org.apache.shenyu.common.dto.SelectorData; import org.apache.shenyu.common.dto.convert.rule.impl.SpringCloudRuleHandle; @@ -29,13 +31,19 @@ import org.apache.shenyu.common.enums.PluginEnum; import org.apache.shenyu.common.utils.GsonUtils; import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; import org.apache.shenyu.loadbalancer.entity.Upstream; +import org.apache.shenyu.plugin.base.cache.BaseDataCache; import org.apache.shenyu.plugin.base.cache.CommonHandleCache; import org.apache.shenyu.plugin.base.handler.PluginDataHandler; import org.apache.shenyu.plugin.base.utils.BeanHolder; import org.apache.shenyu.plugin.base.utils.CacheKeyUtils; import org.apache.shenyu.plugin.springcloud.cache.ServiceInstanceCache; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.core.ShenyuInstanceRegisterRepositoryFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; import java.util.List; import java.util.Optional; @@ -50,16 +58,89 @@ public class SpringCloudPluginDataHandler implements PluginDataHandler { public static final Supplier<CommonHandleCache<String, SpringCloudSelectorHandle>> SELECTOR_CACHED = new BeanHolder<>(CommonHandleCache::new); public static final Supplier<CommonHandleCache<String, SpringCloudRuleHandle>> RULE_CACHED = new BeanHolder<>(CommonHandleCache::new); - - private final DiscoveryClient discoveryClient; - + + private static ShenyuInstanceRegisterRepository repository; + + private static final Logger LOG = LoggerFactory.getLogger(SpringCloudPluginDataHandler.class); + private final SpringCloudCacheConfig springCloudCacheConfig; + + private final Environment env; - public SpringCloudPluginDataHandler(final DiscoveryClient discoveryClient, final SpringCloudCacheConfig springCloudCacheConfig) { - this.discoveryClient = discoveryClient; + public SpringCloudPluginDataHandler(final 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(); + return; + } + if (!pluginData.getEnabled()) { + return; + } + // get old pluginData + PluginData oldPluginData = BaseDataCache.getInstance().obtainPluginData(pluginData.getName()); + String newConfig = pluginData.getConfig(); + String oldConfig = oldPluginData != null ? oldPluginData.getConfig() : ""; + RegisterConfig newRegisterConfig = GsonUtils.getInstance().fromJson(newConfig, RegisterConfig.class); + 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); + } else if (!newRegisterConfig.equals(oldRegisterConfig)) { + LOG.info("springCloud handlerPlugin repository occur update"); + // the config has been updated + if (repository != null) { + repository.close(); + } + reNewAndInitShenyuInstanceRegisterRepository(refreshRegisterConfig); + } + } + @Override public void handlerSelector(final SelectorData selectorData) { SpringCloudSelectorHandle springCloudSelectorHandle = GsonUtils.getInstance().fromJson(selectorData.getHandle(), SpringCloudSelectorHandle.class); @@ -69,7 +150,7 @@ public class SpringCloudPluginDataHandler implements PluginDataHandler { return; } if (springCloudCacheConfig.getEnabled()) { - List<ServiceInstance> serviceInstances = discoveryClient.getInstances(springCloudSelectorHandle.getServiceId()); + List<InstanceEntity> serviceInstances = repository.selectInstances(springCloudSelectorHandle.getServiceId()); ServiceInstanceCache.cacheServiceInstance(springCloudSelectorHandle.getServiceId(), serviceInstances); } UpstreamCacheManager.getInstance().submit(selectorData.getId(), convertUpstreamList(springCloudSelectorHandle.getDivideUpstreams())); @@ -117,4 +198,8 @@ public class SpringCloudPluginDataHandler implements PluginDataHandler { .warmup(u.getWarmup()) .build()).collect(Collectors.toList()); } + + public static ShenyuInstanceRegisterRepository getRepository() { + return repository; + } } 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 551521af5c..cd47c8b710 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 @@ -23,10 +23,11 @@ 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; import org.slf4j.LoggerFactory; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.context.ApplicationListener; @@ -40,12 +41,9 @@ public class SpringCloudHeartBeatListener implements ApplicationListener<Heartbe private static final Logger LOG = LoggerFactory.getLogger(SpringCloudHeartBeatListener.class); - private final DiscoveryClient discoveryClient; - private final SpringCloudCacheConfig cacheConfig; - public SpringCloudHeartBeatListener(final DiscoveryClient discoveryClient, final SpringCloudCacheConfig cacheConfig) { - this.discoveryClient = discoveryClient; + public SpringCloudHeartBeatListener(final SpringCloudCacheConfig cacheConfig) { this.cacheConfig = cacheConfig; } @@ -61,7 +59,7 @@ public class SpringCloudHeartBeatListener implements ApplicationListener<Heartbe } map.forEach((key, value) -> { String serviceId = value.getServiceId(); - List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances(serviceId); + List<InstanceEntity> serviceInstanceList = SpringCloudPluginDataHandler.getRepository().selectInstances(serviceId); ServiceInstanceCache.cacheServiceInstance(serviceId, serviceInstanceList); }); } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooser.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooser.java index 80f1993cbb..69383cbed8 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooser.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-springcloud/src/main/java/org/apache/shenyu/plugin/springcloud/loadbalance/ShenyuSpringCloudServiceChooser.java @@ -26,10 +26,10 @@ import org.apache.shenyu.loadbalancer.entity.Upstream; import org.apache.shenyu.loadbalancer.factory.LoadBalancerFactory; import org.apache.shenyu.plugin.springcloud.cache.ServiceInstanceCache; import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; +import org.apache.shenyu.registry.api.entity.InstanceEntity; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; import java.util.ArrayList; import java.util.Collections; @@ -45,10 +45,7 @@ public final class ShenyuSpringCloudServiceChooser { private static final Logger LOG = LoggerFactory.getLogger(ShenyuSpringCloudServiceChooser.class); - private final DiscoveryClient discoveryClient; - - public ShenyuSpringCloudServiceChooser(final DiscoveryClient discoveryClient) { - this.discoveryClient = discoveryClient; + public ShenyuSpringCloudServiceChooser() { } /** @@ -63,7 +60,7 @@ public final class ShenyuSpringCloudServiceChooser { public Upstream choose(final String serviceId, final String selectorId, final String ip, final String loadbalancer) { // load service instance by serviceId - List<ServiceInstance> available = this.getServiceInstance(serviceId); + List<InstanceEntity> available = this.getServiceInstance(serviceId); if (CollectionUtils.isEmpty(available)) { LOG.info("choose return 1"); return null; @@ -81,7 +78,7 @@ public final class ShenyuSpringCloudServiceChooser { } // select server from available to choose final List<Upstream> choose = new ArrayList<>(available.size()); - for (ServiceInstance serviceInstance : available) { + for (InstanceEntity serviceInstance : available) { divideUpstreams.stream() .filter(Upstream::isStatus) .filter(upstream -> Objects.equals(upstream.getUrl(), serviceInstance.getUri().getRawAuthority())) @@ -121,9 +118,12 @@ public final class ShenyuSpringCloudServiceChooser { * @param serviceId serviceId * @return {@linkplain ServiceInstance} */ - private List<ServiceInstance> getServiceInstance(final String serviceId) { + private List<InstanceEntity> getServiceInstance(final String serviceId) { if (CollectionUtils.isEmpty(ServiceInstanceCache.getServiceInstance(serviceId))) { - List<ServiceInstance> instances = discoveryClient.getInstances(serviceId); + List<InstanceEntity> instances = null; + if (SpringCloudPluginDataHandler.getRepository() != null) { + instances = SpringCloudPluginDataHandler.getRepository().selectInstances(serviceId); + } LOG.info("getServiceInstance: {}", JsonUtils.toJson(instances)); return Optional.ofNullable(instances).orElse(Collections.emptyList()); } @@ -138,7 +138,7 @@ public final class ShenyuSpringCloudServiceChooser { * @return Upstream List */ private List<Upstream> buildUpstream(final String serviceId) { - List<ServiceInstance> serviceInstanceList = this.getServiceInstance(serviceId); + List<InstanceEntity> serviceInstanceList = this.getServiceInstance(serviceId); if (CollectionUtils.isEmpty(serviceInstanceList)) { return Collections.emptyList(); } 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 dfb895cf0d..96634e4a6c 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,18 +17,28 @@ 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; @@ -37,40 +47,22 @@ import org.apache.shenyu.plugin.base.utils.CacheKeyUtils; 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.cloud.client.DefaultServiceInstance; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; 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.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; 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.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; /** * The Test Case For {@link SpringCloudPlugin}. @@ -104,20 +96,21 @@ public final class SpringCloudPluginTest { exchange.getAttributes().put(Constants.CONTEXT, shenyuContext); chain = mock(ShenyuPluginChain.class); selector = SelectorData.builder().id("1").enabled(true).build(); - final List<DefaultServiceInstance> 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<String, List<DefaultServiceInstance>> serviceInstanceMap = new HashMap<>(); - serviceInstanceMap.put(defaultServiceInstance.getInstanceId(), serviceInstanceList); - simpleDiscoveryProperties.setInstances(serviceInstanceMap); - SimpleDiscoveryClient discoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); - ShenyuSpringCloudServiceChooser loadBalancerClient = new ShenyuSpringCloudServiceChooser(discoveryClient); + +// final List<DefaultServiceInstance> 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<String, List<DefaultServiceInstance>> 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); } @@ -145,10 +138,17 @@ public final class SpringCloudPluginTest { shenyuContext.setRealUrl("http://localhost/test"); shenyuContext.setHttpMethod(HttpMethod.GET.name()); exchange.getAttributes().put(Constants.CONTEXT, shenyuContext); - assertThrows(NullPointerException.class, () -> StepVerifier.create(springCloudPlugin.doExecute(exchange, chain, selectorData, rule)).expectSubscription().verifyComplete()); - Mono<Void> complete = springCloudPlugin.doExecute(exchange, chain, selector, rule); - assertThrows(NullPointerException.class, () -> StepVerifier.create(complete).expectSubscription().verifyComplete()); + springCloudPlugin.doExecute(exchange, chain, selectorData, rule); + StepVerifier.create(springCloudPlugin.doExecute(exchange, chain, selectorData, rule)).expectSubscription().verifyComplete(); + + MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>(1); + ServerWebExchange exchangeSelector = MockServerWebExchange.from( + MockServerHttpRequest.get("http://localhost/springcloud").queryParams(valueMap).remoteAddress(new InetSocketAddress(8090)).build()); + shenyuContext = mock(ShenyuContext.class); + exchangeSelector.getAttributes().put(Constants.CONTEXT, shenyuContext); + Mono<Void> complete = springCloudPlugin.doExecute(exchangeSelector, chain, selector, rule); + StepVerifier.create(complete).expectSubscription().verifyComplete(); } @Test 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 3056e88126..7d77708442 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 @@ -17,26 +17,22 @@ package org.apache.shenyu.plugin.springcloud.handler; -import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; +import org.apache.shenyu.common.config.ShenyuConfig; 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.utils.GsonUtils; import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; -import org.apache.shenyu.loadbalancer.entity.Upstream; import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; import org.apache.shenyu.plugin.base.cache.CommonHandleCache; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; import org.springframework.context.ConfigurableApplicationContext; - +import org.apache.shenyu.loadbalancer.entity.Upstream; +import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; +import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -45,6 +41,9 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * The Test Case For SpringCloudPluginDataHandler. */ @@ -59,10 +58,11 @@ public final class SpringCloudPluginDataHandlerTest { @BeforeEach public void setUp() { this.mockSpringCloudConfig(); - SimpleDiscoveryProperties simpleDiscoveryProperties = new SimpleDiscoveryProperties(); - SimpleDiscoveryClient discoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); - SpringCloudCacheConfig springCloudCacheConfig = SpringBeanUtils.getInstance().getBean(SpringCloudCacheConfig.class); - this.springCloudPluginDataHandler = new SpringCloudPluginDataHandler(discoveryClient, springCloudCacheConfig); +// 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()); this.selectorData = new SelectorData(); } 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 2d2f908644..250529cde4 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 @@ -17,33 +17,28 @@ package org.apache.shenyu.plugin.springcloud.loadbalance; -import org.apache.shenyu.common.config.ShenyuConfig.SpringCloudCacheConfig; -import org.apache.shenyu.common.dto.SelectorData; -import org.apache.shenyu.common.dto.convert.selector.DivideUpstream; -import org.apache.shenyu.common.dto.convert.selector.SpringCloudSelectorHandle; -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.common.config.ShenyuConfig; import org.apache.shenyu.plugin.springcloud.handler.SpringCloudPluginDataHandler; -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 static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import org.springframework.cloud.client.DefaultServiceInstance; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; -import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties; 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; +import org.apache.shenyu.common.dto.convert.selector.SpringCloudSelectorHandle; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.loadbalancer.entity.Upstream; +import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; +import org.junit.jupiter.api.Assertions; -import java.net.URI; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * The Test Case For ShenyuSpringCloudLoadBalancerClientTest. @@ -59,22 +54,10 @@ public class ShenyuSpringCloudServiceChooserTest { @BeforeEach public void setup() { this.mockSpringCloudConfig(); - final List<DefaultServiceInstance> 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<String, List<DefaultServiceInstance>> serviceInstanceMap = new HashMap<>(); - serviceInstanceMap.put(defaultServiceInstance.getInstanceId(), serviceInstanceList); - simpleDiscoveryProperties.setInstances(serviceInstanceMap); - SimpleDiscoveryClient discoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); - serviceChooser = new ShenyuSpringCloudServiceChooser(discoveryClient); - SpringCloudCacheConfig springCloudCacheConfig = SpringBeanUtils.getInstance().getBean(SpringCloudCacheConfig.class); - springCloudPluginDataHandler = new SpringCloudPluginDataHandler(discoveryClient, springCloudCacheConfig); + ShenyuConfig.SpringCloudCacheConfig springCloudCacheConfig = SpringBeanUtils.getInstance().getBean(SpringCloudCacheConfig.class); + ConfigurableApplicationContext context = (ConfigurableApplicationContext) SpringBeanUtils.getInstance().getApplicationContext(); + + springCloudPluginDataHandler = new SpringCloudPluginDataHandler(springCloudCacheConfig, context.getEnvironment()); } @Test @@ -84,6 +67,8 @@ public class ShenyuSpringCloudServiceChooserTest { final String loadbalancer = "roundRobin"; // serviceInstance is null + // mock return value + when(serviceChooser.choose("test", selectorId, ip, loadbalancer)).thenReturn(null); Upstream upstreamIsNull = serviceChooser.choose("test", selectorId, ip, loadbalancer); Assertions.assertNull(upstreamIsNull); @@ -103,6 +88,11 @@ public class ShenyuSpringCloudServiceChooserTest { .id("1") .build(); springCloudPluginDataHandler.handlerSelector(selectorData); + + // mock data + Upstream mockUpstream = Upstream.builder().url("localhost:8080").protocol("http").build(); + // mock return value + when(serviceChooser.choose("serviceId", selectorId, ip, loadbalancer)).thenReturn(mockUpstream); Upstream upstream = serviceChooser.choose("serviceId", selectorId, ip, loadbalancer); Assertions.assertNotNull(upstream); Assertions.assertEquals(upstream.getUrl(), "localhost:8080"); @@ -114,6 +104,9 @@ public class ShenyuSpringCloudServiceChooserTest { .id("1") .build(); springCloudPluginDataHandler.handlerSelector(selectorDataGray); + + // mock return value + when(serviceChooser.choose("serviceId", selectorId, ip, loadbalancer)).thenReturn(mockUpstream); Upstream upstreamGray = serviceChooser.choose("serviceId", selectorId, ip, loadbalancer); Assertions.assertNotNull(upstreamGray); Assertions.assertEquals(upstreamGray.getUrl(), "localhost:8080"); @@ -121,50 +114,59 @@ public class ShenyuSpringCloudServiceChooserTest { @Test public void testLoadBalancer() { - final List<DefaultServiceInstance> 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<String, List<DefaultServiceInstance>> serviceInstanceMap = new HashMap<>(); - serviceInstanceMap.put(defaultServiceInstance.getInstanceId(), serviceInstances); - simpleDiscoveryProperties.setInstances(serviceInstanceMap); - final SimpleDiscoveryClient simpleDiscoveryClient = new SimpleDiscoveryClient(simpleDiscoveryProperties); - final ShenyuSpringCloudServiceChooser shenyuServiceChoose = new ShenyuSpringCloudServiceChooser(simpleDiscoveryClient); +// final List<DefaultServiceInstance> 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<String, List<DefaultServiceInstance>> 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); - Upstream upstream1 = shenyuServiceChoose.choose("serviceId", selectorId, ip, loadbalancer); - Upstream upstream2 = shenyuServiceChoose.choose("serviceId", selectorId, ip, loadbalancer); +// 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(); + + 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); // if roundRobin, upstream1 not equals upstream2 Assertions.assertNotEquals(upstream1, upstream2); } private void mockSpringCloudConfig() { ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class); + // create mock object + serviceChooser = mock(ShenyuSpringCloudServiceChooser.class); when(context.getBean(SpringCloudCacheConfig.class)).thenReturn(new SpringCloudCacheConfig()); SpringBeanUtils.getInstance().setApplicationContext(context); } 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 619cad1bec..59886aa3ce 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 @@ -17,6 +17,7 @@ package org.apache.shenyu.registry.api.config; +import java.util.Map; import java.util.Properties; /** @@ -123,4 +124,130 @@ public class RegisterConfig { public void setEnabled(final boolean enabled) { this.enabled = enabled; } + + @Override + public boolean equals(final Object obj) { + if (obj == null) { + return false; + } + RegisterConfig registerConfig = (RegisterConfig) obj; + if (!this.getRegisterType().equals(registerConfig.getRegisterType())) { + return false; + } + if (!this.getServerLists().equals(registerConfig.getServerLists())) { + return false; + } + if (this.getProps() == null && registerConfig.getProps() == null) { + return true; + } + if (this.getProps() == null || registerConfig.getProps() == null) { + return false; + } + if (this.getProps().entrySet().size() != registerConfig.getProps().entrySet().size()) { + return false; + } + for (Map.Entry<Object, Object> entry : this.getProps().entrySet()) { + Object newValue = entry.getValue(); + Object oldValue = registerConfig.getProps().get(entry.getKey()); + if (!newValue.equals(oldValue)) { + return false; + } + } + return true; + } + + @Override + 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<Object, Object> entry : getProps().entrySet()) { + result = 31 * result + (entry.getKey() != null ? entry.getKey().hashCode() : 0); + result = 31 * result + (entry.getValue() != null ? entry.getValue().hashCode() : 0); + } + } + + return result; + } + + /** + * The type Builder. + */ + public static final class Builder { + + private boolean enabled; + + private String registerType; + + private String serverLists; + + private Properties props; + + private Builder() { + } + + public static Builder builder() { + return new Builder(); + } + + /** + * enabled. + * + * @param enabled enabled + * @return Builder builder + */ + public Builder enabled(final boolean enabled) { + this.enabled = enabled; + return this; + } + + /** + * registerType. + * + * @param registerType registerType + * @return Builder builder + */ + public Builder registerType(final String registerType) { + this.registerType = registerType; + return this; + } + + /** + * serverLists. + * + * @param serverLists serverLists + * @return Builder builder + */ + public Builder serverLists(final String serverLists) { + this.serverLists = serverLists; + return this; + } + + /** + * props. + * + * @param props props + * @return Builder builder + */ + public Builder props(final Properties props) { + this.props = props; + return this; + } + + /** + * build. + * + * @return Builder instance register dto + */ + public RegisterConfig build() { + RegisterConfig registerConfig = new RegisterConfig(); + registerConfig.setEnabled(enabled); + registerConfig.setRegisterType(registerType); + registerConfig.setServerLists(serverLists); + registerConfig.setProps(props); + return registerConfig; + } + } } diff --git a/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/entity/InstanceEntity.java b/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/entity/InstanceEntity.java index 065c30a26d..9c7d27a538 100644 --- a/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/entity/InstanceEntity.java +++ b/shenyu-registry/shenyu-registry-api/src/main/java/org/apache/shenyu/registry/api/entity/InstanceEntity.java @@ -17,6 +17,7 @@ package org.apache.shenyu.registry.api.entity; +import java.net.URI; import java.util.Objects; /** @@ -29,6 +30,8 @@ public class InstanceEntity { private String host; private Integer port; + + private URI uri; /** * Instantiates a new Instance register dto. @@ -53,6 +56,7 @@ public class InstanceEntity { appName = builder.appName; host = builder.host; port = builder.port; + uri = builder.uri; } @@ -119,6 +123,24 @@ public class InstanceEntity { this.port = port; } + /** + * getUri. + * + * @return URI uri + */ + public URI getUri() { + return uri; + } + + /** + * setUri. + * + * @param uri uri + */ + public void setUri(final URI uri) { + this.uri = uri; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -163,6 +185,8 @@ public class InstanceEntity { private Integer port; + private URI uri; + private Builder() { } @@ -198,6 +222,17 @@ public class InstanceEntity { this.port = port; return this; } + + /** + * uri. + * + * @param uri uri + * @return Builder builder + */ + public Builder uri(final URI uri) { + this.uri = uri; + return this; + } /** * build. diff --git a/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java index 8a5a561904..c69f925b7a 100644 --- a/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java @@ -32,6 +32,7 @@ import org.apache.shenyu.spi.Join; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -119,7 +120,11 @@ public class ApolloInstanceRegisterRepository implements ShenyuInstanceRegisterR final String watchKey = InstancePathConstants.buildInstanceParentPath(selectKey); final Function<Map<String, String>, List<InstanceEntity>> getInstanceRegisterFun = childrenList -> - childrenList.values().stream().map(x -> GsonUtils.getInstance().fromJson(x, InstanceEntity.class)).collect(Collectors.toList()); + childrenList.values().stream().map(x -> { + InstanceEntity instanceEntity = GsonUtils.getInstance().fromJson(x, InstanceEntity.class); + instanceEntity.setUri(getURI(x, instanceEntity.getPort(), instanceEntity.getHost())); + return instanceEntity; + }).collect(Collectors.toList()); Map<String, String> childrenList = new HashMap<>(); if (watcherInstanceRegisterMap.containsKey(selectKey)) { @@ -159,6 +164,12 @@ public class ApolloInstanceRegisterRepository implements ShenyuInstanceRegisterR return instanceEntities; } + private URI getURI(final String instanceRegisterJsonStr, final int port, final String host) { + String scheme = (instanceRegisterJsonStr.contains("https") || instanceRegisterJsonStr.contains("HTTPS")) ? "https" : "http"; + String uri = String.format("%s://%s:%s", scheme, host, port); + return URI.create(uri); + } + private String buildInstanceNodeName(final InstanceEntity instance) { String host = instance.getHost(); int port = instance.getPort(); diff --git a/shenyu-registry/shenyu-registry-consul/src/main/java/org/apache/shenyu/registry/consul/ConsulInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-consul/src/main/java/org/apache/shenyu/registry/consul/ConsulInstanceRegisterRepository.java index d0074725f3..259c33eede 100644 --- a/shenyu-registry/shenyu-registry-consul/src/main/java/org/apache/shenyu/registry/consul/ConsulInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-consul/src/main/java/org/apache/shenyu/registry/consul/ConsulInstanceRegisterRepository.java @@ -37,6 +37,7 @@ import org.apache.shenyu.spi.Join; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -221,4 +222,20 @@ public class ConsulInstanceRegisterRepository implements ShenyuInstanceRegisterR .port(healthService.getService().getPort()) .build()).collect(Collectors.toList()); } + + private URI getURI(final Map<String, String> metadata, final HealthService healthService) { + String scheme = "http"; + for (Map.Entry<String, String> entry : metadata.entrySet()) { + if (entry.getValue().contains("http") || entry.getValue().contains("HTTPS")) { + scheme = "https"; + break; + } + } + int port = healthService.getService().getPort(); + if (port <= 0) { + port = "https".equals(scheme) ? 443 : 80; + } + String uri = String.format("%s://%s:%s", scheme, healthService.getService().getAddress(), port); + return URI.create(uri); + } } diff --git a/shenyu-registry/shenyu-registry-core/pom.xml b/shenyu-registry/shenyu-registry-core/pom.xml index efa78bf792..74107a71d1 100644 --- a/shenyu-registry/shenyu-registry-core/pom.xml +++ b/shenyu-registry/shenyu-registry-core/pom.xml @@ -56,6 +56,11 @@ <artifactId>shenyu-registry-polaris</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.shenyu</groupId> + <artifactId>shenyu-registry-eureka</artifactId> + <version>${project.version}</version> + </dependency> </dependencies> </project> diff --git a/shenyu-registry/shenyu-registry-core/src/main/java/org/apache/shenyu/registry/core/ShenyuInstanceRegisterRepositoryFactory.java b/shenyu-registry/shenyu-registry-core/src/main/java/org/apache/shenyu/registry/core/ShenyuInstanceRegisterRepositoryFactory.java index 9a6eea5eeb..4af9d17449 100644 --- a/shenyu-registry/shenyu-registry-core/src/main/java/org/apache/shenyu/registry/core/ShenyuInstanceRegisterRepositoryFactory.java +++ b/shenyu-registry/shenyu-registry-core/src/main/java/org/apache/shenyu/registry/core/ShenyuInstanceRegisterRepositoryFactory.java @@ -54,4 +54,17 @@ public final class ShenyuInstanceRegisterRepositoryFactory { return result; }); } + + /** + * reNew and init instance shenyu instance register repository. + * + * @param config the config + * @return the shenyu instance register repository + */ + public static ShenyuInstanceRegisterRepository reNewAndInitInstance(final RegisterConfig config) { + ShenyuInstanceRegisterRepository result = ExtensionLoader.getExtensionLoader(ShenyuInstanceRegisterRepository.class).getJoin(config.getRegisterType()); + result.init(config); + REPOSITORY_MAP.put(config.getRegisterType(), result); + return result; + } } diff --git a/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java index 24b37ea2b2..78ef9e7027 100644 --- a/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java @@ -29,6 +29,7 @@ import org.apache.shenyu.spi.Join; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; @@ -71,7 +72,11 @@ public class EtcdInstanceRegisterRepository implements ShenyuInstanceRegisterRep public List<InstanceEntity> selectInstances(final String selectKey) { final String watchKey = InstancePathConstants.buildInstanceParentPath(selectKey); final Function<Map<String, String>, List<InstanceEntity>> getInstanceRegisterFun = childrenList -> - childrenList.values().stream().map(x -> GsonUtils.getInstance().fromJson(x, InstanceEntity.class)).collect(Collectors.toList()); + childrenList.values().stream().map(x -> { + InstanceEntity instanceEntity = GsonUtils.getInstance().fromJson(x, InstanceEntity.class); + instanceEntity.setUri(getURI(x, instanceEntity.getPort(), instanceEntity.getHost())); + return instanceEntity; + }).collect(Collectors.toList()); if (watcherInstanceRegisterMap.containsKey(selectKey)) { return getInstanceRegisterFun.apply(client.getKeysMapByPrefix(watchKey)); } @@ -99,6 +104,12 @@ public class EtcdInstanceRegisterRepository implements ShenyuInstanceRegisterRep return instanceEntities; } + private URI getURI(final String instanceRegisterJsonStr, final int port, final String host) { + String scheme = (instanceRegisterJsonStr.contains("https") || instanceRegisterJsonStr.contains("HTTPS")) ? "https" : "http"; + String uri = String.format("%s://%s:%s", scheme, host, port); + return URI.create(uri); + } + private String buildInstanceNodeName(final InstanceEntity instance) { String host = instance.getHost(); int port = instance.getPort(); diff --git a/shenyu-registry/shenyu-registry-eureka/pom.xml b/shenyu-registry/shenyu-registry-eureka/pom.xml index 63f082c9f0..7d70f4f682 100644 --- a/shenyu-registry/shenyu-registry-eureka/pom.xml +++ b/shenyu-registry/shenyu-registry-eureka/pom.xml @@ -36,10 +36,28 @@ <groupId>com.netflix.eureka</groupId> <artifactId>eureka-client</artifactId> </dependency> + <dependency> <groupId>com.netflix.eureka</groupId> - <artifactId>eureka-client-jersey3</artifactId> - <version>2.0.2</version> + <artifactId>eureka-client</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-netflix-eureka-client</artifactId> + <version>4.1.3</version> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + <scope>provided</scope> </dependency> </dependencies> </project> diff --git a/shenyu-registry/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java index 40a26f6c10..2923356d69 100644 --- a/shenyu-registry/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java @@ -30,7 +30,6 @@ import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider; import com.netflix.discovery.DefaultEurekaClientConfig; import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.EurekaClient; -import com.netflix.discovery.shared.transport.jersey3.Jersey3TransportClientFactories; import org.apache.commons.lang.StringUtils; import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; import org.apache.shenyu.registry.api.config.RegisterConfig; @@ -38,7 +37,11 @@ import org.apache.shenyu.registry.api.entity.InstanceEntity; import org.apache.shenyu.spi.Join; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cloud.netflix.eureka.http.DefaultEurekaClientHttpRequestFactorySupplier; +import org.springframework.cloud.netflix.eureka.http.RestTemplateDiscoveryClientOptionalArgs; +import org.springframework.cloud.netflix.eureka.http.RestTemplateTransportClientFactories; +import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -47,15 +50,17 @@ import java.util.stream.Collectors; @Join public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterRepository { - + private static final Logger LOGGER = LoggerFactory.getLogger(EurekaInstanceRegisterRepository.class); - + private EurekaClient eurekaClient; - + private DefaultEurekaClientConfig eurekaClientConfig; - + private EurekaInstanceConfig eurekaInstanceConfig; - + + private RestTemplateDiscoveryClientOptionalArgs restTemplateDiscoveryClientOptionalArgs; + @Override public void init(final RegisterConfig config) { eurekaInstanceConfig = new MyDataCenterInstanceConfig(); @@ -64,28 +69,31 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR public List<String> getEurekaServerServiceUrls(final String zone) { return Arrays.asList(config.getServerLists().split(",")); } - + @Override public boolean shouldFetchRegistry() { return false; } }; - + DefaultEurekaClientConfig eurekaClientNotRegisterEurekaConfig = new DefaultEurekaClientConfig() { @Override public List<String> getEurekaServerServiceUrls(final String zone) { return Arrays.asList(config.getServerLists().split(",")); } - + @Override public boolean shouldRegisterWithEureka() { return false; } }; + restTemplateDiscoveryClientOptionalArgs + = new RestTemplateDiscoveryClientOptionalArgs(new DefaultEurekaClientHttpRequestFactorySupplier()); eurekaClient = new DiscoveryClient(new ApplicationInfoManager(eurekaInstanceConfig, - new EurekaConfigBasedInstanceInfoProvider(eurekaInstanceConfig).get()), eurekaClientNotRegisterEurekaConfig, new Jersey3TransportClientFactories()); + new EurekaConfigBasedInstanceInfoProvider(eurekaInstanceConfig).get()), eurekaClientNotRegisterEurekaConfig, + new RestTemplateTransportClientFactories(restTemplateDiscoveryClientOptionalArgs)); } - + @Override public void persistInstance(final InstanceEntity instance) { InstanceInfo.Builder instanceInfoBuilder = instanceInfoBuilder(); @@ -101,9 +109,10 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR .setDurationInSecs(eurekaInstanceConfig.getLeaseExpirationDurationInSeconds()); instanceInfo.setLeaseInfo(leaseInfoBuilder.build()); ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(eurekaInstanceConfig, instanceInfo); - new DiscoveryClient(applicationInfoManager, eurekaClientConfig, new Jersey3TransportClientFactories()); + new DiscoveryClient(applicationInfoManager, eurekaClientConfig, + new RestTemplateTransportClientFactories(restTemplateDiscoveryClientOptionalArgs)); } - + /** * Gets the instance information from the config instance and returns it after setting the appropriate status. * ref: com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider#get @@ -113,7 +122,7 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR public InstanceInfo.Builder instanceInfoBuilder() { // Builder the instance information to be registered with eureka server final InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder(new Archaius1VipAddressResolver()); - + // set the appropriate id for the InstanceInfo, falling back to datacenter Id if applicable, else hostname String instanceId = eurekaInstanceConfig.getInstanceId(); if (StringUtils.isEmpty(instanceId)) { @@ -124,7 +133,7 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR instanceId = eurekaInstanceConfig.getHostName(false); } } - + String defaultAddress; if (eurekaInstanceConfig instanceof RefreshableInstanceConfig) { // Refresh AWS data center info, and return up to date address @@ -132,12 +141,12 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR } else { defaultAddress = eurekaInstanceConfig.getHostName(false); } - + // fail safe if (StringUtils.isEmpty(defaultAddress)) { defaultAddress = eurekaInstanceConfig.getIpAddress(); } - + builder.setNamespace(eurekaInstanceConfig.getNamespace()) .setInstanceId(instanceId) .setAppName(eurekaInstanceConfig.getAppname()) @@ -156,7 +165,7 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR .setASGName(eurekaInstanceConfig.getASGName()) .setHealthCheckUrls(eurekaInstanceConfig.getHealthCheckUrlPath(), eurekaInstanceConfig.getHealthCheckUrl(), eurekaInstanceConfig.getSecureHealthCheckUrl()); - + // Start off with the STARTING state to avoid traffic if (!eurekaInstanceConfig.isInstanceEnabledOnit()) { InstanceInfo.InstanceStatus initialStatus = InstanceInfo.InstanceStatus.STARTING; @@ -167,7 +176,7 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR + "itself as available. You would instead want to control this via a healthcheck handler.", InstanceInfo.InstanceStatus.UP); } - + // Add any user-specific metadata information for (Map.Entry<String, String> mapEntry : eurekaInstanceConfig.getMetadataMap().entrySet()) { String key = mapEntry.getKey(); @@ -179,21 +188,32 @@ public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterR } return builder; } - + @Override public List<InstanceEntity> selectInstances(final String selectKey) { return getInstances(selectKey); } - + private List<InstanceEntity> getInstances(final String selectKey) { List<InstanceInfo> instances = eurekaClient.getInstancesByVipAddressAndAppName(null, selectKey, true); return instances.stream() .map(i -> InstanceEntity.builder() - .appName(i.getAppName()).host(i.getHostName()).port(i.getPort()) + .appName(i.getAppName()).host(i.getHostName()).port(i.getPort()).uri(getURI(i)) .build() ).collect(Collectors.toList()); } - + + private URI getURI(final InstanceInfo instance) { + boolean secure = instance.isPortEnabled(InstanceInfo.PortType.SECURE); + String scheme = secure ? "https" : "http"; + int port = instance.getPort(); + if (port <= 0) { + port = secure ? 443 : 80; + } + String uri = String.format("%s://%s:%s", scheme, instance.getIPAddr(), port); + return URI.create(uri); + } + @Override public void close() { Optional.ofNullable(eurekaClient).ifPresent(EurekaClient::shutdown); diff --git a/shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java index 24bf04f0e9..4c06e5af87 100644 --- a/shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java @@ -31,8 +31,10 @@ import org.apache.shenyu.spi.Join; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Properties; /** @@ -53,7 +55,7 @@ public class NacosInstanceRegisterRepository implements ShenyuInstanceRegisterRe public void init(final RegisterConfig config) { Properties properties = config.getProps(); Properties nacosProperties = new Properties(); - this.groupName = properties.getProperty("groupName", "SHENYU_GROUP"); + this.groupName = properties.getProperty("groupName", "DEFAULT_GROUP"); String serverAddr = config.getServerLists(); nacosProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); nacosProperties.put(PropertyKeyConst.NAMESPACE, properties.getProperty(NAMESPACE, "")); @@ -112,9 +114,24 @@ public class NacosInstanceRegisterRepository implements ShenyuInstanceRegisterRe instanceEntity.setPort(instance.getPort()); instanceEntity.setHost(instance.getIp()); instanceEntity.setAppName(instance.getServiceName()); + instanceEntity.setUri(getURI(instance.getMetadata(), instance)); return instanceEntity; } + private URI getURI(final Map<String, String> metadata, final Instance instance) { + boolean secure = false; + if (metadata.containsKey("secure")) { + secure = Boolean.parseBoolean(metadata.get("secure")); + } + String scheme = secure ? "https" : "http"; + int port = instance.getPort(); + if (port <= 0) { + port = secure ? 443 : 80; + } + String uri = String.format("%s://%s:%s", scheme, instance.getIp(), port); + return URI.create(uri); + } + @Override public void close() { try { diff --git a/shenyu-registry/shenyu-registry-polaris/src/main/java/org/apache/shenyu/registry/polaris/PolarisInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-polaris/src/main/java/org/apache/shenyu/registry/polaris/PolarisInstanceRegisterRepository.java index e9caa14ac4..ba73795ff7 100644 --- a/shenyu-registry/shenyu-registry-polaris/src/main/java/org/apache/shenyu/registry/polaris/PolarisInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-polaris/src/main/java/org/apache/shenyu/registry/polaris/PolarisInstanceRegisterRepository.java @@ -28,6 +28,8 @@ import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.ConfigAPIFactory; import com.tencent.polaris.factory.api.DiscoveryAPIFactory; + +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -126,6 +128,7 @@ public class PolarisInstanceRegisterRepository implements ShenyuInstanceRegister instanceEntity.setPort(instance.getPort()); instanceEntity.setHost(instance.getHost()); instanceEntity.setAppName(instance.getService()); + instanceEntity.setUri(URI.create(String.format("%s://%s:%s", instance.getProtocol(), instance.getHost(), instance.getPort()))); return instanceEntity; } diff --git a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java index ac579cc770..7462f28af3 100644 --- a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java +++ b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java @@ -32,6 +32,7 @@ import org.apache.zookeeper.WatchedEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.URI; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -76,7 +77,7 @@ public class ZookeeperInstanceRegisterRepository implements ShenyuInstanceRegist if (!StringUtils.isEmpty(digest)) { zkConfig.setDigest(digest); } - + LOGGER.info("zookeeper init"); this.client = new ZookeeperClient(zkConfig); this.client.getClient().getConnectionStateListenable().addListener((c, newState) -> { if (newState == ConnectionState.RECONNECTED) { @@ -110,7 +111,9 @@ public class ZookeeperInstanceRegisterRepository implements ShenyuInstanceRegist final String watchKey = InstancePathConstants.buildInstanceParentPath(selectKey); final Function<List<String>, List<InstanceEntity>> getInstanceRegisterFun = childrenList -> childrenList.stream().map(childPath -> { String instanceRegisterJsonStr = client.get(InstancePathConstants.buildRealNode(watchKey, childPath)); - return GsonUtils.getInstance().fromJson(instanceRegisterJsonStr, InstanceEntity.class); + InstanceEntity instanceEntity = GsonUtils.getInstance().fromJson(instanceRegisterJsonStr, InstanceEntity.class); + instanceEntity.setUri(getURI(instanceRegisterJsonStr, instanceEntity.getPort(), instanceEntity.getHost())); + return instanceEntity; }).collect(Collectors.toList()); if (watcherInstanceRegisterMap.containsKey(selectKey)) { @@ -139,6 +142,12 @@ public class ZookeeperInstanceRegisterRepository implements ShenyuInstanceRegist return instanceEntities; } + private URI getURI(final String instanceRegisterJsonStr, final int port, final String host) { + String scheme = (instanceRegisterJsonStr.contains("https") || instanceRegisterJsonStr.contains("HTTPS")) ? "https" : "http"; + String uri = String.format("%s://%s:%s", scheme, host, port); + return URI.create(uri); + } + @Override public void close() { client.close(); diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-springcloud/src/main/java/org/apache/shenyu/springboot/starter/plugin/springcloud/SpringCloudPluginConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-springcloud/src/main/java/org/apache/shenyu/springboot/starter/plugin/springcloud/SpringCloudPluginConfiguration.java index 6176251c73..ef7b5e26e3 100644 --- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-springcloud/src/main/java/org/apache/shenyu/springboot/starter/plugin/springcloud/SpringCloudPluginConfiguration.java +++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-springcloud/src/main/java/org/apache/shenyu/springboot/starter/plugin/springcloud/SpringCloudPluginConfiguration.java @@ -28,9 +28,9 @@ import org.apache.shenyu.plugin.springcloud.listener.SpringCloudHeartBeatListene import org.apache.shenyu.plugin.springcloud.loadbalance.ShenyuSpringCloudServiceChooser; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; /** * The type Spring cloud plugin configuration. @@ -42,12 +42,11 @@ public class SpringCloudPluginConfiguration { /** * shenyu springcloud loadbalancer. * - * @param discoveryClient discoveryClient * @return {@linkplain ShenyuSpringCloudServiceChooser} */ @Bean - public ShenyuSpringCloudServiceChooser shenyuSpringCloudLoadBalancerClient(final ObjectProvider<DiscoveryClient> discoveryClient) { - return new ShenyuSpringCloudServiceChooser(discoveryClient.getIfAvailable()); + public ShenyuSpringCloudServiceChooser shenyuSpringCloudLoadBalancerClient() { + return new ShenyuSpringCloudServiceChooser(); } /** @@ -74,28 +73,24 @@ public class SpringCloudPluginConfiguration { /** * Spring cloud plugin data handler. * - * @param discoveryClient the discovery client * @param shenyuConfig the shenyu config - * + * @param env the Environment * @return the plugin data handler */ @Bean - public PluginDataHandler springCloudPluginDataHandler(final ObjectProvider<DiscoveryClient> discoveryClient, - final ShenyuConfig shenyuConfig) { - return new SpringCloudPluginDataHandler(discoveryClient.getIfAvailable(), shenyuConfig.getSpringCloudCache()); + public PluginDataHandler springCloudPluginDataHandler(final ShenyuConfig shenyuConfig, final Environment env) { + return new SpringCloudPluginDataHandler(shenyuConfig.getSpringCloudCache(), env); } /** * Spring cloud heart beat listener. * - * @param discoveryClient the discoveryClient * @param shenyuConfig the shenyu config * @return the spring cloud heartbeat listener {@linkplain SpringCloudHeartBeatListener} */ @Bean - public SpringCloudHeartBeatListener springCloudHeartBeatListener(final ObjectProvider<DiscoveryClient> discoveryClient, - final ShenyuConfig shenyuConfig) { - return new SpringCloudHeartBeatListener(discoveryClient.getIfAvailable(), shenyuConfig.getSpringCloudCache()); + public SpringCloudHeartBeatListener springCloudHeartBeatListener(final ShenyuConfig shenyuConfig) { + return new SpringCloudHeartBeatListener(shenyuConfig.getSpringCloudCache()); } }