From 63c575018179aa60c11d3e2d4e55e4d6e0de90ab Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Thu, 21 Sep 2023 11:49:32 +0800 Subject: [PATCH] https://gitee.com/cskefu/cskefu/issues/I836RO add checks on start Signed-off-by: Hai Liang Wang --- .../java/com/cskefu/cc/basic/Constants.java | 2 +- .../cskefu/cc/config/QuotaWdClientConfig.java | 38 +++++++++++++++++++ .../com/cskefu/cc/proxy/LicenseProxy.java | 33 +++++++++++++++- .../src/main/resources/application.properties | 2 + contact-center/root/pom.xml | 2 +- docker-compose.yml | 1 + sample.env | 1 + 7 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 contact-center/app/src/main/java/com/cskefu/cc/config/QuotaWdClientConfig.java diff --git a/contact-center/app/src/main/java/com/cskefu/cc/basic/Constants.java b/contact-center/app/src/main/java/com/cskefu/cc/basic/Constants.java index 85ca0eb4..82e5f3db 100644 --- a/contact-center/app/src/main/java/com/cskefu/cc/basic/Constants.java +++ b/contact-center/app/src/main/java/com/cskefu/cc/basic/Constants.java @@ -223,7 +223,7 @@ public class Constants { public static final String LICENSE_SERVER_INST_ID = "SERVERINSTID"; public static final String LICENSE_SERVICE_NAME = "SERVICENAME"; public static final String LICENSE_SERVICE_NAME_PREFIX = "春松客服"; - public static final String LICENSES = "LICENSES"; + public static final String LICENSEIDS = "LICENSEIDS"; public static final String METAKV_DATATYPE_STRING = "string"; } diff --git a/contact-center/app/src/main/java/com/cskefu/cc/config/QuotaWdClientConfig.java b/contact-center/app/src/main/java/com/cskefu/cc/config/QuotaWdClientConfig.java new file mode 100644 index 00000000..f2613424 --- /dev/null +++ b/contact-center/app/src/main/java/com/cskefu/cc/config/QuotaWdClientConfig.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. + * , Licensed under the Chunsong Public + * License, Version 1.0 (the "License"), https://docs.cskefu.com/licenses/v1.html + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * Copyright (C) 2019-2022 Chatopera Inc, , + * Licensed under the Apache License, Version 2.0, + * http://www.apache.org/licenses/LICENSE-2.0 + */ +package com.cskefu.cc.config; + +import com.chatopera.store.sdk.QuotaWdClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class QuotaWdClientConfig { + + @Value("${cskefu.license.store}") + private String cskefuLicenseStore; + + /** + * 证书商店服务客户端 + * + * @return + */ + @Bean + public QuotaWdClient quotaWdClient() { + QuotaWdClient quotaWdClient = new QuotaWdClient(); + quotaWdClient.setBaseUrl(cskefuLicenseStore); + return quotaWdClient; + } +} diff --git a/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java b/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java index eb9c00f6..e3b3a622 100644 --- a/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java +++ b/contact-center/app/src/main/java/com/cskefu/cc/proxy/LicenseProxy.java @@ -10,7 +10,11 @@ */ package com.cskefu.cc.proxy; +import com.chatopera.store.sdk.QuotaWdClient; +import com.chatopera.store.sdk.Response; +import com.chatopera.store.sdk.exceptions.InvalidResponseException; import com.cskefu.cc.basic.Constants; +import com.cskefu.cc.basic.MainContext; import com.cskefu.cc.basic.MainUtils; import com.cskefu.cc.model.Metakv; import com.cskefu.cc.persistence.repository.MetakvRepository; @@ -19,6 +23,7 @@ import org.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; import org.springframework.stereotype.Service; import java.util.Date; @@ -31,11 +36,35 @@ public class LicenseProxy { @Autowired private MetakvRepository metkvRes; + @Autowired + private QuotaWdClient quotaWdClient; + /** * 初始化 serverinstId * serverinstId 作为服务唯一的实例ID */ public void checkOnStartup() { + /** + * Check service connection + */ + System.out.println("[license] license service URL " + quotaWdClient.getBaseUrl()); + try { + Response resp = quotaWdClient.ping(); + System.out.println("[license] license service ping successfully."); + + if (resp.getRc() != 0) { + throw new InvalidResponseException("Unexpected response from license service " + resp.toString()); + } + } catch (InvalidResponseException e) { + logger.error("[license] make sure this host machine could connect to " + quotaWdClient.getBaseUrl() + " during running."); + logger.error("[license] checkOnStartup could not connect to license service, CSKeFu instance is terminated.", e); + // Very serious event happens, just shutdown the instance + SpringApplication.exit(MainContext.getContext(), () -> 1); + } + + /** + * Init local data for License + */ Optional metaServerinstIdOpt = metkvRes.findFirstByMetakey(Constants.LICENSE_SERVER_INST_ID); if (metaServerinstIdOpt.isEmpty()) { // 没有 serverinstId 信息,初始化 @@ -50,10 +79,10 @@ public class LicenseProxy { createMetakv(Constants.LICENSE_SERVICE_NAME, serviceName, Constants.METAKV_DATATYPE_STRING); } - Optional metaLicensesOpt = metkvRes.findFirstByMetakey(Constants.LICENSES); + Optional metaLicensesOpt = metkvRes.findFirstByMetakey(Constants.LICENSEIDS); if (metaLicensesOpt.isEmpty()) { // 没有 license 信息,初始化 - createMetakv(Constants.LICENSES, (new JSONArray()).toString(), Constants.METAKV_DATATYPE_STRING); + createMetakv(Constants.LICENSEIDS, (new JSONArray()).toString(), Constants.METAKV_DATATYPE_STRING); } } diff --git a/contact-center/app/src/main/resources/application.properties b/contact-center/app/src/main/resources/application.properties index f39822a5..3dc2ee0b 100644 --- a/contact-center/app/src/main/resources/application.properties +++ b/contact-center/app/src/main/resources/application.properties @@ -154,6 +154,8 @@ cskefu.modules.report=true # https://gitlab.chatopera.com/chatopera/cosinee/issues/838 cskefu.settings.webim.visitor-separate=false +# License Service Provider URL +cskefu.license.store=https://store.chatopera.com ############################################## # Skype Channel diff --git a/contact-center/root/pom.xml b/contact-center/root/pom.xml index c1e1b160..08b85210 100644 --- a/contact-center/root/pom.xml +++ b/contact-center/root/pom.xml @@ -410,7 +410,7 @@ com.chatopera.store store-sdk - 1.0-SNAPSHOT + 1.1-SNAPSHOT diff --git a/docker-compose.yml b/docker-compose.yml index fd2cde82..32f5423b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -42,6 +42,7 @@ services: - BOT_THRESHOLD_FAQ_BEST_REPLY=${BOT_THRESHOLD_FAQ_BEST_REPLY:-0.9} - BOT_THRESHOLD_FAQ_SUGG_REPLY=${BOT_THRESHOLD_FAQ_SUGG_REPLY:-0.1} - CSKEFU_SETTINGS_WEBIM_VISITOR_SEPARATE=true + - CSKEFU_LICENSE_STORE=${CSKEFU_LICENSE_STORE:-https://store.chatopera.com} - TONGJI_BAIDU_SITEKEY=${TONGJI_BAIDU_SITEKEY:-placeholder} - EXTRAS_LOGIN_BANNER=${NOTICE_LOGIN_BANNER:-off} - EXTRAS_LOGIN_CHATBOX=${EXTRAS_LOGIN_CHATBOX:-off} diff --git a/sample.env b/sample.env index e41e7d5d..7f2a6136 100644 --- a/sample.env +++ b/sample.env @@ -30,6 +30,7 @@ CACHE_SETUP_STRATEGY=create_by_force BOT_THRESHOLD_FAQ_BEST_REPLY=0.8 BOT_THRESHOLD_FAQ_SUGG_REPLY=0.6 +CSKEFU_LICENSE_STORE=https://store.chatopera.com TONGJI_BAIDU_SITEKEY=placeholder EXTRAS_LOGIN_BANNER="" EXTRAS_LOGIN_CHATBOX=