diff --git a/.github/actions/common/spring/action.yml b/.github/actions/common/spring/action.yml index 01db91ead7..04d7dbe7a5 100644 --- a/.github/actions/common/spring/action.yml +++ b/.github/actions/common/spring/action.yml @@ -57,3 +57,11 @@ runs: key: ${{ runner.os }}-agent-${{ github.run_id }} - name: plugin change check uses: ./.github/actions/common/plugin-change-check + - name: change file version + # For build feign client ssl call config, according to springCloud version discriminate high and low config. + if: matrix.springCloudVersion == '2020.0.0' || matrix.springCloudVersion == '2021.0.0' || matrix.springCloudVersion == '2021.0.3' + shell: bash + run: | + mv sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationLowVersion.java sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationLowVersion.java.lowVersion + mv sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationHighVersion.java.highVersion sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationHighVersion.java + ls sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration \ No newline at end of file diff --git a/.github/actions/scenarios/spring/graceful/action.yml b/.github/actions/scenarios/spring/graceful/action.yml index a131d30cbc..04b8e2f6d9 100644 --- a/.github/actions/scenarios/spring/graceful/action.yml +++ b/.github/actions/scenarios/spring/graceful/action.yml @@ -19,12 +19,15 @@ runs: mvn package -Dspring.cloud.version=${{ matrix.springCloudVersion }} -Dspring.boot.version=${{ matrix.springBootVersion }} -DskipTests -P common-test${{ env.tailVersion }} --file sermant-integration-tests/spring-test/pom.xml - name: start provider that has closed graceful ability shell: bash + env: + servicecomb.service.enableSpringRegister: true + servicecomb.service.preferIpAddress: true run: | nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=feign -Dserver.port=8014 \ -Dsermant_log_dir=${{ env.logDir }}/feign-provider-8014${{ env.tailVersion }} -jar \ sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign${{ env.tailVersion }}/feign-provider${{ env.tailVersion }}/target/feign-provider${{ env.tailVersion }}.jar > ${{ env.logDir }}/feign-provider-8014.log 2>&1 & nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=rest -Dserver.port=8004 -jar \ - -Dsermant_log_dir=${{ env.logDir }}/rest-provider${{ env.tailVersion }} \ + -Dsermant_log_dir=${{ env.logDir }}/rest-provider-8004${{ env.tailVersion }} \ sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/target/rest-provider.jar > ${{ env.logDir }}/rest-provider-8004.log 2>&1 & - name: start applications that has opened graceful ability shell: bash @@ -34,9 +37,12 @@ runs: grace.rule.enableGraceShutdown: true grace.rule.enableOfflineNotify: true grace.rule.warmUpTime: 600 + servicecomb.service.enableSpringRegister: true + servicecomb.service.preferIpAddress: true + # graceful-rest-provider service port 8443 do not change, it special for springCloud Edgware.SR2 test ssl feature. run: | cp sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign${{ env.tailVersion }}/feign-provider${{ env.tailVersion }}/target/feign-provider${{ env.tailVersion }}.jar graceful-feign-provider${{ env.tailVersion }}.jar - nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=feign -Dserver.port=8013 -jar \ + nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=feign -Dserver.port=8013 -Dserver.ssl.enabled=true -Dregister.service.secure=true -jar \ -Dsermant_log_dir=${{ env.logDir }}/feign-provider${{ env.tailVersion }} -Dgrace.rule.httpServerPort=16777 \ graceful-feign-provider${{ env.tailVersion }}.jar > ${{ env.logDir }}/feign-provider.log 2>&1 & nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=feign -Dserver.port=8015 -jar \ @@ -44,7 +50,7 @@ runs: sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign${{ env.tailVersion }}/feign-consumer${{ env.tailVersion }}/target/feign-consumer${{ env.tailVersion }}.jar > ${{ env.logDir }}/feign-consumer.log 2>&1 & cp sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/target/rest-provider.jar graceful-rest-provider.jar - nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=rest -Dserver.port=8003 -jar \ + nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=rest -Dserver.port=8443 -Dserver.ssl.enabled=true -Dregister.service.secure=true -jar \ -Dsermant_log_dir=${{ env.logDir }}/rest-provider${{ env.tailVersion }} -Dgrace.rule.httpServerPort=16779 \ graceful-rest-provider.jar > ${{ env.logDir }}/rest-provider.log 2>&1 & nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar=appName=default -Dservice.meta.application=rest -Dserver.port=8005 -jar \ diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/pom.xml b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/pom.xml index af6d93d0ca..cf40369ac7 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/pom.xml +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/pom.xml @@ -41,5 +41,9 @@ 1.0.0 provided + + org.apache.httpcomponents + httpclient + diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/src/main/java/com/huaweicloud/spring/feign/api/HeaderMatchConfiguration.java b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/src/main/java/com/huaweicloud/spring/feign/api/HeaderMatchConfiguration.java index b70fb50324..e1f13895c5 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/src/main/java/com/huaweicloud/spring/feign/api/HeaderMatchConfiguration.java +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-api-1.5.x/src/main/java/com/huaweicloud/spring/feign/api/HeaderMatchConfiguration.java @@ -17,9 +17,28 @@ package com.huaweicloud.spring.feign.api; +import feign.Client; +import feign.Feign; import feign.RequestInterceptor; import feign.RequestTemplate; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.netflix.feign.ribbon.CachingSpringLoadBalancerFactory; +import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.context.annotation.Bean; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + /** * 针对header方法增加请求头判断是否可以匹配成功 * @@ -27,7 +46,9 @@ * @since 2022-07-29 */ public class HeaderMatchConfiguration implements RequestInterceptor { + private static final Logger LOGGER = LoggerFactory.getLogger(HeaderMatchConfiguration.class); private static final String KEY = "key"; + private static SSLSocketFactory feignSocketFactory = null; @Override public void apply(RequestTemplate template) { @@ -46,4 +67,69 @@ public void apply(RequestTemplate template) { template.header(KEY, "100"); } } + + /** + * 构建Feign Builder + * + * @param lbClientFactory LB工厂 + * @param clientFactory client工厂 + * @return Feign.Builder + */ + @Bean + public Feign.Builder feignBuilder(CachingSpringLoadBalancerFactory lbClientFactory, + SpringClientFactory clientFactory) { + final Client sslClient = client(lbClientFactory, clientFactory); + return Feign.builder().client(sslClient); + } + + /** + * 构建Feign client + * + * @param lbClientFactory LB工厂 + * @param clientFactory client工厂 + * @return client + */ + @Bean + public Client client(CachingSpringLoadBalancerFactory lbClientFactory, SpringClientFactory clientFactory) { + if (feignSocketFactory == null) { + try { + feignSocketFactory = getFeignSslSocketFactory(); + } catch (NoSuchAlgorithmException e) { + LOGGER.error("build ssl feign client failed for NoSuchAlgorithmException"); + } catch (KeyManagementException e) { + LOGGER.error("build ssl feign client failed for KeyManagementException"); + } + } + return new LoadBalancerFeignClient(new Client.Default(feignSocketFactory, new NoopHostnameVerifier()), + lbClientFactory, clientFactory); + } + + private SSLSocketFactory getFeignSslSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { + TrustManager[] trustManagers = new TrustManager[1]; + TrustManager tm = new SslTrustManager(); + trustManagers[0] = tm; + SSLContext sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, trustManagers, null); + return sslContext.getSocketFactory(); + } + + /** + * 构建SSL Manager + * + * @since 2022-07-29 + */ + static class SslTrustManager implements TrustManager, X509TrustManager { + @Override + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { + } + + @Override + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } } diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/application.yml b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/application.yml index 28ad84a86c..1b3b6fc9c2 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/application.yml +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/application.yml @@ -1,5 +1,11 @@ server: port: 8003 + ssl: + enabled: false + key-store: classpath:private.pkcs12 + key-store-password: 123456 + key-store-type: pkcs12 + spring: application: name: feign-provider diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/private.pkcs12 b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/private.pkcs12 new file mode 100644 index 0000000000..235d539481 Binary files /dev/null and b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign-1.5.x/feign-provider-1.5.x/src/main/resources/private.pkcs12 differ diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/pom.xml b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/pom.xml index ab7bf62b93..d2685d8e26 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/pom.xml +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/pom.xml @@ -40,5 +40,9 @@ 1.0.0 provided + + org.apache.httpcomponents + httpclient + diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientConfigSslUtils.java b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientConfigSslUtils.java new file mode 100644 index 0000000000..dde685a323 --- /dev/null +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientConfigSslUtils.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + */ + +package com.huaweicloud.spring.feign.api.configuration; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +/** + * 构建feignClient SSL调用的FeignSocketFactory工具类 + * + * @author chengyouling + * @since 2023-02-10 + */ +public class FeignClientConfigSslUtils { + private FeignClientConfigSslUtils() { + } + + /** + * 构建FeignSslSocketFactory + * + * @return SSLSocketFactory + * @throws NoSuchAlgorithmException NoSuchAlgorithmException + * @throws KeyManagementException KeyManagementException + */ + public static SSLSocketFactory getFeignSslSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { + TrustManager[] trustManagers = new TrustManager[1]; + TrustManager tm = new FeignClientConfigSslUtils.SslTrustManager(); + trustManagers[0] = tm; + SSLContext sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, trustManagers, null); + return sslContext.getSocketFactory(); + } + + /** + * 构建SSL Manager + * + * @since 2022-07-29 + */ + static class SslTrustManager implements TrustManager, X509TrustManager { + @Override + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { + } + + @Override + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } +} diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationHighVersion.java.highVersion b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationHighVersion.java.highVersion new file mode 100644 index 0000000000..0b95132f9d --- /dev/null +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationHighVersion.java.highVersion @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + */ + +package com.huaweicloud.spring.feign.api.configuration; + +import feign.Client; +import feign.Feign; + +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties; +import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; +import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +import javax.net.ssl.SSLSocketFactory; + +/** + * 针对springCloud 2020.0.0/2021.0.0/2021.0.3高版本FeignClient SSL请求证书认证处理 + * + * @author chengyouling + * @since 2023-02-10 + */ +@Configuration +public class FeignClientSslConfigurationHighVersion { + private static SSLSocketFactory feignSocketFactory = null; + private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientSslConfigurationHighVersion.class); + + /** + * 构建Feign Builder + * + * @param loadBalancerClient loadBalancerClient + * @param properties balancerProperties + * @param factory clientFactory + * @return Client + */ + @Bean + public Feign.Builder feignBuilder(LoadBalancerClient loadBalancerClient, LoadBalancerProperties properties, + LoadBalancerClientFactory factory) { + final Client sslClient = feignClient(loadBalancerClient, properties, factory); + return Feign.builder().client(sslClient); + } + + /** + * 构建Feign client + * + * @param loadBalancerClient loadBalancerClient + * @param properties balancerProperties + * @param factory clientFactory + * @return Client + */ + @Bean + public Client feignClient(LoadBalancerClient loadBalancerClient, LoadBalancerProperties properties, + LoadBalancerClientFactory factory) { + if (feignSocketFactory == null) { + try { + feignSocketFactory = FeignClientConfigSslUtils.getFeignSslSocketFactory(); + } catch (NoSuchAlgorithmException e) { + LOGGER.error("build ssl feign client failed for NoSuchAlgorithmException"); + } catch (KeyManagementException e) { + LOGGER.error("build ssl feign client failed for KeyManagementException"); + } + } + return new FeignBlockingLoadBalancerClient(new Client.Default(feignSocketFactory, new NoopHostnameVerifier()), + loadBalancerClient, properties, factory); + } +} diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationLowVersion.java b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationLowVersion.java new file mode 100644 index 0000000000..331d22be81 --- /dev/null +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-api/src/main/java/com/huaweicloud/spring/feign/api/configuration/FeignClientSslConfigurationLowVersion.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + */ + +package com.huaweicloud.spring.feign.api.configuration; + +import feign.Client; +import feign.Feign; + +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; +import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +import javax.net.ssl.SSLSocketFactory; + +/** + * 针对springCloud 2020.0.0/2021.0.0/2021.0.3以下低版本版本FeignClient SSL请求证书认证处理 + * + * @author chengyouling + * @since 2023-02-10 + */ +@Configuration +public class FeignClientSslConfigurationLowVersion { + private static SSLSocketFactory feignSocketFactory = null; + private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientSslConfigurationLowVersion.class); + + /** + * 构建Feign Builder + * + * @param lbClientFactory LB工厂 + * @param clientFactory client工厂 + * @return Feign.Builder + */ + @Bean + public Feign.Builder feignBuilder(CachingSpringLoadBalancerFactory lbClientFactory, + SpringClientFactory clientFactory) { + final Client sslClient = client(lbClientFactory, clientFactory); + return Feign.builder().client(sslClient); + } + + /** + * 构建Feign client + * + * @param lbClientFactory LB工厂 + * @param clientFactory client工厂 + * @return client + */ + @Bean + public Client client(CachingSpringLoadBalancerFactory lbClientFactory, SpringClientFactory clientFactory) { + if (feignSocketFactory == null) { + try { + feignSocketFactory = FeignClientConfigSslUtils.getFeignSslSocketFactory(); + } catch (NoSuchAlgorithmException e) { + LOGGER.error("build ssl feign client failed for NoSuchAlgorithmException"); + } catch (KeyManagementException e) { + LOGGER.error("build ssl feign client failed for KeyManagementException"); + } + } + return new LoadBalancerFeignClient(new Client.Default(feignSocketFactory, new NoopHostnameVerifier()), + lbClientFactory, clientFactory); + } +} diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-consumer/src/main/java/com/huaweicloud/spring/feign/consumer/FeignConsumerApplication.java b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-consumer/src/main/java/com/huaweicloud/spring/feign/consumer/FeignConsumerApplication.java index 422eee11b7..19b25f7c12 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-consumer/src/main/java/com/huaweicloud/spring/feign/consumer/FeignConsumerApplication.java +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-consumer/src/main/java/com/huaweicloud/spring/feign/consumer/FeignConsumerApplication.java @@ -32,7 +32,8 @@ */ @SpringBootApplication(scanBasePackages = { "com.huaweicloud.spring.feign.consumer.controller", - "com.huaweicloud.spring.common.loadbalancer.feign" + "com.huaweicloud.spring.common.loadbalancer.feign", + "com.huaweicloud.spring.feign.api.configuration" }) @EnableFeignClients(basePackages = "com.huaweicloud.spring.feign.api") public class FeignConsumerApplication { diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/application.yml b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/application.yml index 28ad84a86c..1b3b6fc9c2 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/application.yml +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/application.yml @@ -1,5 +1,11 @@ server: port: 8003 + ssl: + enabled: false + key-store: classpath:private.pkcs12 + key-store-password: 123456 + key-store-type: pkcs12 + spring: application: name: feign-provider diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/private.pkcs12 b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/private.pkcs12 new file mode 100644 index 0000000000..235d539481 Binary files /dev/null and b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-feign/feign-provider/src/main/resources/private.pkcs12 differ diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-consumer/src/main/java/com/huaweicloud/spring/rest/consumer/FlowcontrolConiguration.java b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-consumer/src/main/java/com/huaweicloud/spring/rest/consumer/FlowcontrolConiguration.java index 732747145a..d71f6217d0 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-consumer/src/main/java/com/huaweicloud/spring/rest/consumer/FlowcontrolConiguration.java +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-consumer/src/main/java/com/huaweicloud/spring/rest/consumer/FlowcontrolConiguration.java @@ -17,11 +17,28 @@ package com.huaweicloud.spring.rest.consumer; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +import javax.net.ssl.SSLContext; + /** * 流控配置类 * @@ -30,6 +47,9 @@ */ @Configuration public class FlowcontrolConiguration { + private static final Logger LOGGER = LoggerFactory.getLogger(FlowcontrolConiguration.class); + private static final int TIME_OUT = 5 * 60 * 1000; + /** * 注入请求器 * @@ -60,6 +80,35 @@ public RestTemplate routerRestTemplate() { @LoadBalanced @Bean("gracefulRestTemplate") public RestTemplate gracefulRestTemplate() { - return new RestTemplate(); + return buildRestTemplate(); + } + + private RestTemplate buildRestTemplate() { + RestTemplate restTemplate = null; + try { + restTemplate = new RestTemplate(buildHttpRequestFactory()); + } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) { + LOGGER.error("build SSL restTemplate failed!"); + } + return restTemplate; + } + + private HttpComponentsClientHttpRequestFactory buildHttpRequestFactory() + throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { + TrustStrategy strategy = (x509Certificates, authType) -> true; + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, strategy).build(); + SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); + RequestConfig requestConfig = RequestConfig.custom() + .setSocketTimeout(TIME_OUT) + .setConnectTimeout(TIME_OUT) + .setConnectionRequestTimeout(TIME_OUT) + .build(); + HttpClientBuilder httpClientBuilder = HttpClients.custom(); + httpClientBuilder.setDefaultRequestConfig(requestConfig); + httpClientBuilder.setSSLSocketFactory(factory); + CloseableHttpClient httpClient = httpClientBuilder.build(); + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + requestFactory.setHttpClient(httpClient); + return requestFactory; } } diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/application.yml b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/application.yml index a68a329b6c..3ff34c4a49 100644 --- a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/application.yml +++ b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/application.yml @@ -1,5 +1,11 @@ server: port: 8003 + ssl: + enabled: false + key-store: classpath:private.pkcs12 + key-store-password: 123456 + key-store-type: pkcs12 + spring: application: name: rest-provider diff --git a/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/private.pkcs12 b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/private.pkcs12 new file mode 100644 index 0000000000..235d539481 Binary files /dev/null and b/sermant-integration-tests/spring-test/spring-common-demos/spring-common-resttemplate/rest-provider/src/main/resources/private.pkcs12 differ diff --git a/sermant-plugins/sermant-service-registry/config/config.yaml b/sermant-plugins/sermant-service-registry/config/config.yaml index 6fefb576f9..ed7fa77c74 100644 --- a/sermant-plugins/sermant-service-registry/config/config.yaml +++ b/sermant-plugins/sermant-service-registry/config/config.yaml @@ -2,6 +2,7 @@ register.service: registerType: SERVICE_COMB # 注册服务类型,支持SERVICE_COMB/NACOS # 注册服务地址,service_comb:http://localhost:30100;nacos:127.0.0.1:8848 address: http://localhost:30100 + secure: false # 是否开启SSL,true为是,服务发现返回实例协议为https,false为否,服务发现返回实例协议为http servicecomb.service: heartbeatInterval: 15 # 服务实例心跳发送间隔(单位:秒) openMigration: false # 是否开启迁移功能 diff --git a/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/ConfigConstants.java b/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/ConfigConstants.java index 71a5ee7e8d..28c875d341 100644 --- a/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/ConfigConstants.java +++ b/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/ConfigConstants.java @@ -59,6 +59,11 @@ public class ConfigConstants { */ public static final String COMMON_FRAMEWORK = "Sermant"; + /** + * 服务是否加密公共参数 + */ + public static final String SECURE = "secure"; + private ConfigConstants() { } } diff --git a/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/RegisterServiceCommonConfig.java b/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/RegisterServiceCommonConfig.java index f2a3d50893..fd02a941ee 100644 --- a/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/RegisterServiceCommonConfig.java +++ b/sermant-plugins/sermant-service-registry/registry-common/src/main/java/com/huawei/registry/config/RegisterServiceCommonConfig.java @@ -42,6 +42,11 @@ public class RegisterServiceCommonConfig implements PluginConfig { */ private String address = "http://127.0.0.1:30100"; + /** + * 是否加密 + */ + private boolean secure; + public RegisterType getRegisterType() { return registerType; } @@ -76,4 +81,12 @@ public List getAddressList() { } return addressList; } + + public boolean isSecure() { + return secure; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistration.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistration.java index c311552935..33aee4fe4f 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistration.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistration.java @@ -60,12 +60,16 @@ public int getPort() { @Override public boolean isSecure() { - return false; + return microServiceInstance.isSecure(); } @Override public URI getUri() { - return URI.create(String.format(Locale.ENGLISH, "http://%s:%s", getHost(), getPort())); + String format = "http://%s:%s"; + if (microServiceInstance.isSecure()) { + format = "https://%s:%s"; + } + return URI.create(String.format(Locale.ENGLISH, format, getHost(), getPort())); } @Override diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistry.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistry.java index b4b6ca7bcb..1e9229183b 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistry.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombRegistry.java @@ -18,10 +18,12 @@ package com.huawei.registry.auto.sc; import com.huawei.registry.config.GraceConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.config.grace.GraceHelper; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.entity.FixedResult; import com.huawei.registry.services.RegisterCenterService; +import com.huawei.registry.utils.CommonUtils; import com.huawei.registry.utils.ZoneUtils; import com.huaweicloud.sermant.core.common.LoggerFactory; @@ -101,7 +103,9 @@ private RegisterCenterService getRegisterCenterService() { private void fillClientInfo(Registration registration) { RegisterContext.INSTANCE.getClientInfo().setHost(registration.getHost()); - RegisterContext.INSTANCE.getClientInfo().setMeta(registration.getMetadata()); + RegisterServiceCommonConfig config = PluginConfigManager.getPluginConfig(RegisterServiceCommonConfig.class); + RegisterContext.INSTANCE.getClientInfo().setMeta(CommonUtils.putSecureToMetaData(registration.getMetadata(), + config)); RegisterContext.INSTANCE.getClientInfo().setPort(registration.getPort()); RegisterContext.INSTANCE.getClientInfo().setServiceId(registration.getServiceId()); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServer.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServer.java index 897f6be66a..ae94bd14cb 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServer.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServer.java @@ -40,7 +40,8 @@ public class ServiceCombServer extends Server { * @param microServiceInstance 实例信息 */ public ServiceCombServer(MicroServiceInstance microServiceInstance) { - super(microServiceInstance.getIp(), microServiceInstance.getPort()); + super(microServiceInstance.isSecure() ? "https" : "http", microServiceInstance.getIp(), + microServiceInstance.getPort()); this.microServiceInstance = microServiceInstance; } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServerIntrospector.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServerIntrospector.java index 36b16c0b0d..2d2b03e01c 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServerIntrospector.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceCombServerIntrospector.java @@ -17,6 +17,8 @@ package com.huawei.registry.auto.sc; +import com.huawei.registry.config.ConfigConstants; + import com.netflix.loadbalancer.Server; import org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector; @@ -37,4 +39,13 @@ public Map getMetadata(Server server) { } return super.getMetadata(server); } + + @Override + public boolean isSecure(Server server) { + if (server instanceof ServiceCombServer) { + Map metadata = getMetadata(server); + return metadata != null && "true".equalsIgnoreCase(metadata.get(ConfigConstants.SECURE)); + } + return super.isSecure(server); + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceInstanceHolder.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceInstanceHolder.java index aaa4404e72..442699044a 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceInstanceHolder.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/auto/sc/ServiceInstanceHolder.java @@ -17,6 +17,7 @@ package com.huawei.registry.auto.sc; +import com.huawei.registry.config.ConfigConstants; import com.huawei.registry.config.RegistrationProperties; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.entity.MicroServiceInstance; @@ -91,4 +92,9 @@ public String getInstanceId() { public Map getMetadata() { return this.metadata; } + + @Override + public boolean isSecure() { + return Boolean.valueOf(metadata.get(ConfigConstants.SECURE)); + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java index 46134cf100..d21a51552b 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java @@ -18,9 +18,11 @@ package com.huawei.registry.config; import com.huawei.registry.context.RegisterContext; +import com.huawei.registry.utils.CommonUtils; import com.huawei.registry.utils.HostUtils; import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -74,7 +76,8 @@ public void init() { RegisterContext.INSTANCE.getClientInfo().setServiceName(serviceName); RegisterContext.INSTANCE.getClientInfo().setServiceId(serviceName); RegisterContext.INSTANCE.getClientInfo().setPort(port); - RegisterContext.INSTANCE.getClientInfo().setMeta(new HashMap<>()); + RegisterServiceCommonConfig config = PluginConfigManager.getPluginConfig(RegisterServiceCommonConfig.class); + RegisterContext.INSTANCE.getClientInfo().setMeta(CommonUtils.putSecureToMetaData(new HashMap<>(), config)); RegisterContext.INSTANCE.getClientInfo().setZone( environment.getProperty(SpringRegistryConstants.SPRING_LOAD_BALANCER_ZONE)); configureHostIp(); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/DiscoveryServiceInstance.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/DiscoveryServiceInstance.java index d0905a3b27..b3128a85a3 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/DiscoveryServiceInstance.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/DiscoveryServiceInstance.java @@ -68,12 +68,16 @@ public int getPort() { @Override public boolean isSecure() { - return false; + return microServiceInstance.isSecure(); } @Override public URI getUri() { - return URI.create(String.format(Locale.ENGLISH, "http://%s:%s", getHost(), getPort())); + String format = "http://%s:%s"; + if (microServiceInstance.isSecure()) { + format = "https://%s:%s"; + } + return URI.create(String.format(Locale.ENGLISH, format, getHost(), getPort())); } public String getId() { diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/MicroServiceInstance.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/MicroServiceInstance.java index c039bf8e32..806a60180b 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/MicroServiceInstance.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/MicroServiceInstance.java @@ -74,4 +74,11 @@ public interface MicroServiceInstance { * @return 元数据 */ Map getMetadata(); + + /** + * 是否加密 + * + * @return 是否加密 + */ + boolean isSecure(); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/ScServer.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/ScServer.java index 94380b6829..f297ae2d79 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/ScServer.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/entity/ScServer.java @@ -41,7 +41,8 @@ public class ScServer extends Server { * @param serviceName 服务名 */ public ScServer(final MicroServiceInstance microServiceInstance, String serviceName) { - super(microServiceInstance.getIp(), microServiceInstance.getPort()); + super(microServiceInstance.isSecure() ? "https" : "http", microServiceInstance.getIp(), + microServiceInstance.getPort()); this.microServiceInstance = microServiceInstance; this.serviceName = serviceName; } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/RegistrationInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/RegistrationInterceptor.java index 3a5a8a3951..e8f83d0ac8 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/RegistrationInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/RegistrationInterceptor.java @@ -17,11 +17,13 @@ package com.huawei.registry.interceptors; import com.huawei.registry.config.GraceConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.config.grace.GraceHelper; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.entity.FixedResult; import com.huawei.registry.services.RegisterCenterService; import com.huawei.registry.support.RegisterSwitchSupport; +import com.huawei.registry.utils.CommonUtils; import com.huawei.registry.utils.ZoneUtils; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; @@ -60,7 +62,9 @@ public ExecuteContext doBefore(ExecuteContext context) { private void fillClientInfo(Registration registration) { RegisterContext.INSTANCE.getClientInfo().setHost(registration.getHost()); - RegisterContext.INSTANCE.getClientInfo().setMeta(registration.getMetadata()); + RegisterServiceCommonConfig config = PluginConfigManager.getPluginConfig(RegisterServiceCommonConfig.class); + RegisterContext.INSTANCE.getClientInfo().setMeta(CommonUtils.putSecureToMetaData(registration.getMetadata(), + config)); RegisterContext.INSTANCE.getClientInfo().setPort(registration.getPort()); RegisterContext.INSTANCE.getClientInfo().setServiceId(registration.getServiceId()); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/utils/CommonUtils.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/utils/CommonUtils.java index b4ec7ec3fe..561ac728da 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/utils/CommonUtils.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/utils/CommonUtils.java @@ -17,8 +17,13 @@ package com.huawei.registry.utils; +import com.huawei.registry.config.ConfigConstants; +import com.huawei.registry.config.RegisterServiceCommonConfig; + import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.utils.StringUtils; +import java.util.Map; import java.util.Optional; import java.util.function.Consumer; import java.util.logging.Logger; @@ -114,4 +119,19 @@ public static void accept(Consumer consumer, T target) { public static void sleep(long timeMs) { accept(SLEEP, timeMs); } + + /** + * meta数据中增加secure配置参数 + * + * @param meta meta数据 + * @param config 公共注册配置 + * @return meta数据 + */ + public static Map putSecureToMetaData(Map meta, + RegisterServiceCommonConfig config) { + if (StringUtils.isEmpty(meta.get(ConfigConstants.SECURE))) { + meta.put(ConfigConstants.SECURE, String.valueOf(config.isSecure())); + } + return meta; + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombRegistryTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombRegistryTest.java index 52180a6cef..27f4f53412 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombRegistryTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombRegistryTest.java @@ -17,6 +17,7 @@ package com.huawei.registry.auto.sc; import com.huawei.registry.config.RegisterConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.context.RegisterContext.ClientInfo; import com.huawei.registry.entity.FixedResult; @@ -68,6 +69,8 @@ public void setUp() { pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig( + RegisterServiceCommonConfig.class)).thenReturn(new RegisterServiceCommonConfig()); } @After diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombServerTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombServerTest.java new file mode 100644 index 0000000000..77629a58ed --- /dev/null +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/ServiceCombServerTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package com.huawei.registry.auto.sc; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.huawei.registry.entity.MicroServiceInstance; + +/** + * ServiceComb服务测试 + * + * @author chengyouling + * @since 2023-01-10 + */ +public class ServiceCombServerTest { + private final String serviceName = "test"; + + @Test + public void testScheme() { + ServiceCombServer serviceCombServer = new ServiceCombServer(buildInstance(8001, true)); + Assert.assertEquals(serviceCombServer.getScheme(), "https"); + ServiceCombServer serverHttp = new ServiceCombServer(buildInstance(8001, false)); + Assert.assertEquals(serverHttp.getScheme(), "http"); + } + + /** + * 构建实例 + * + * @param port 端口 + * @return 实例 + */ + public MicroServiceInstance buildInstance(int port, boolean secure) { + return new MicroServiceInstance() { + @Override + public String getServiceName() { + return serviceName; + } + + @Override + public String getHost() { + return "localhost"; + } + + @Override + public String getIp() { + return "127.0.0.1"; + } + + @Override + public int getPort() { + return port; + } + + @Override + public String getServiceId() { + return serviceName; + } + + @Override + public String getInstanceId() { + return null; + } + + @Override + public Map getMetadata() { + return new HashMap<>(); + } + + @Override + public boolean isSecure() { + return secure; + } + }; + } +} diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/reactive/ServiceCombReactiveDiscoveryClientTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/reactive/ServiceCombReactiveDiscoveryClientTest.java index 7295ab0f49..c31fc9789b 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/reactive/ServiceCombReactiveDiscoveryClientTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/auto/sc/reactive/ServiceCombReactiveDiscoveryClientTest.java @@ -110,6 +110,11 @@ public String getInstanceId() { public Map getMetadata() { return null; } + + @Override + public boolean isSecure() { + return false; + } }; } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/DiscoveryServiceInstanceTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/DiscoveryServiceInstanceTest.java new file mode 100644 index 0000000000..ee418ab138 --- /dev/null +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/DiscoveryServiceInstanceTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package com.huawei.registry.entity; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +/** + * 服务发现实例测试 + * + * @author chengyouling + * @since 2023-01-10 + */ +public class DiscoveryServiceInstanceTest { + private final String serviceName = "test"; + + @Test + public void testScheme() { + DiscoveryServiceInstance instance = new DiscoveryServiceInstance( + buildInstance(8001, true), serviceName); + Assert.assertEquals(instance.getUri().getScheme(), "https"); + DiscoveryServiceInstance instanceHttp = new DiscoveryServiceInstance( + buildInstance(8001, false), serviceName); + Assert.assertEquals(instanceHttp.getUri().getScheme(), "http"); + } + + /** + * 构建实例 + * + * @param port 端口 + * @return 实例 + */ + public MicroServiceInstance buildInstance(int port, boolean secure) { + return new MicroServiceInstance() { + @Override + public String getServiceName() { + return serviceName; + } + + @Override + public String getHost() { + return "localhost"; + } + + @Override + public String getIp() { + return "127.0.0.1"; + } + + @Override + public int getPort() { + return port; + } + + @Override + public String getServiceId() { + return serviceName; + } + + @Override + public String getInstanceId() { + return null; + } + + @Override + public Map getMetadata() { + return new HashMap<>(); + } + + @Override + public boolean isSecure() { + return secure; + } + }; + } +} diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/ScServerTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/ScServerTest.java new file mode 100644 index 0000000000..55197eab98 --- /dev/null +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/entity/ScServerTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package com.huawei.registry.entity; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +/** + * ScServer测试 + * + * @author chengyouling + * @since 2023-01-10 + */ +public class ScServerTest { + private final String serviceName = "test"; + + @Test + public void testScheme() { + ScServer scServer = new ScServer(buildInstance(8001, true), serviceName); + Assert.assertEquals(scServer.getScheme(), "https"); + ScServer scServerHttp = new ScServer(buildInstance(8001, false), serviceName); + Assert.assertEquals(scServerHttp.getScheme(), "http"); + } + + /** + * 构建实例 + * + * @param port 端口 + * @return 实例 + */ + public MicroServiceInstance buildInstance(int port, boolean secure) { + return new MicroServiceInstance() { + @Override + public String getServiceName() { + return serviceName; + } + + @Override + public String getHost() { + return "localhost"; + } + + @Override + public String getIp() { + return "127.0.0.1"; + } + + @Override + public int getPort() { + return port; + } + + @Override + public String getServiceId() { + return serviceName; + } + + @Override + public String getInstanceId() { + return null; + } + + @Override + public Map getMetadata() { + return new HashMap<>(); + } + + @Override + public boolean isSecure() { + return secure; + } + }; + } +} diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java index 8834197ef8..c003659a63 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java @@ -175,6 +175,11 @@ public String getInstanceId() { public Map getMetadata() { return meta; } + + @Override + public boolean isSecure() { + return false; + } }; } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java index 5d2bff78a7..db2b4bb0c6 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java @@ -18,6 +18,7 @@ import com.huawei.registry.config.RegisterConfig; import com.huawei.registry.config.RegisterDynamicConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.context.RegisterContext; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; @@ -40,6 +41,8 @@ public abstract class BaseRegistryTest { protected static final RegisterConfig REGISTER_CONFIG = new RegisterConfig(); + protected static final RegisterServiceCommonConfig COMMON_CONFIG = new RegisterServiceCommonConfig(); + protected static MockedStatic pluginConfigManagerMockedStatic; protected static MockedStatic pluginServiceManagerMockedStatic; @@ -57,6 +60,8 @@ public static void init() { pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(REGISTER_CONFIG); + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig( + RegisterServiceCommonConfig.class)).thenReturn(COMMON_CONFIG); pluginServiceManagerMockedStatic = Mockito.mockStatic(PluginServiceManager.class); serviceManagerMockedStatic = Mockito.mockStatic(ServiceManager.class); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java index 80c38197f6..50a0bbedf7 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java @@ -16,6 +16,7 @@ package com.huawei.registry.interceptors; +import com.huawei.registry.config.RegisterDynamicConfig; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.entity.MicroServiceInstance; import com.huawei.registry.interceptors.cloud3.x.ZookeeperInstanceSupplierInterceptorTest; @@ -80,6 +81,7 @@ public void doBefore() throws NoSuchMethodException { RegisterContext.INSTANCE.setAvailable(true); REGISTER_CONFIG.setEnableSpringRegister(true); REGISTER_CONFIG.setOpenMigration(true); + RegisterDynamicConfig.INSTANCE.setClose(false); final ExecuteContext context = interceptor.doBefore(buildContext(client, new Object[]{serviceName})); Assert.assertTrue(context.isSkip()); Assert.assertTrue(context.getResult() instanceof List); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/RegistrationInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/RegistrationInterceptorTest.java index 6ea2861b74..b668e06f89 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/RegistrationInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/RegistrationInterceptorTest.java @@ -87,7 +87,6 @@ public void updateInstanceStatus(String status) { } }); - } @Test diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/cloud3/x/ZookeeperInstanceSupplierInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/cloud3/x/ZookeeperInstanceSupplierInterceptorTest.java index 7951d631fa..a2bc73451a 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/cloud3/x/ZookeeperInstanceSupplierInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/cloud3/x/ZookeeperInstanceSupplierInterceptorTest.java @@ -188,6 +188,11 @@ public String getInstanceId() { public Map getMetadata() { return new HashMap<>(); } + + @Override + public boolean isSecure() { + return false; + } }; } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/health/NacosHealthInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/health/NacosHealthInterceptorTest.java index 39dc309907..f2685aad52 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/health/NacosHealthInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/health/NacosHealthInterceptorTest.java @@ -24,7 +24,6 @@ import com.fasterxml.jackson.databind.node.LongNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; import org.junit.Assert; import org.junit.Test; diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosClient.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosClient.java index 49d659af76..349b60aef4 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosClient.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosClient.java @@ -64,7 +64,7 @@ public class NacosClient { */ public NacosClient() { nacosRegisterConfig = PluginConfigManager.getPluginConfig(NacosRegisterConfig.class); - nacosServiceManager = new NacosServiceManager(nacosRegisterConfig); + nacosServiceManager = new NacosServiceManager(); nacosServiceDiscovery = new NacosServiceDiscovery(nacosServiceManager); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosServiceDiscovery.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosServiceDiscovery.java index 952b48c3ef..981ef0f47c 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosServiceDiscovery.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/client/NacosServiceDiscovery.java @@ -16,6 +16,7 @@ package com.huawei.registry.service.client; +import com.huawei.registry.config.ConfigConstants; import com.huawei.registry.config.NacosRegisterConfig; import com.huawei.registry.service.register.NacosServiceInstance; import com.huawei.registry.service.register.NacosServiceManager; @@ -41,8 +42,6 @@ public class NacosServiceDiscovery { private static final int DEFAULT_CAPACITY = 16; - private static final String SECURE_KEY = "secure"; - private final NacosRegisterConfig nacosRegisterConfig; private final NacosServiceManager nacosServiceManager; @@ -115,8 +114,8 @@ public Optional convertServiceInstance(Instance instance, metadata.put("nacos.ephemeral", String.valueOf(instance.isEphemeral())); nacosServiceInstance.setMetadata(metadata); - if (metadata.containsKey(SECURE_KEY)) { - boolean secure = Boolean.parseBoolean(metadata.get(SECURE_KEY)); + if (metadata.containsKey(ConfigConstants.SECURE)) { + boolean secure = Boolean.parseBoolean(metadata.get(ConfigConstants.SECURE)); nacosServiceInstance.setSecure(secure); } return Optional.of(nacosServiceInstance); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceInstance.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceInstance.java index d9b9dc9822..28a14aef37 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceInstance.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceInstance.java @@ -94,6 +94,7 @@ public void setSecure(boolean secure) { this.secure = secure; } + @Override public boolean isSecure() { return secure; } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceManager.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceManager.java index 146ca76055..bb856da5ee 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceManager.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/NacosServiceManager.java @@ -16,9 +16,13 @@ package com.huawei.registry.service.register; +import com.huawei.registry.config.ConfigConstants; import com.huawei.registry.config.NacosRegisterConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.context.RegisterContext; +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; + import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingService; @@ -26,6 +30,7 @@ import com.alibaba.nacos.client.naming.NacosNamingMaintainService; import com.alibaba.nacos.client.naming.NacosNamingService; +import java.util.Map; import java.util.Objects; import java.util.Properties; @@ -35,19 +40,22 @@ * @since 2022-10-20 */ public class NacosServiceManager { + private static final String SECURE_KEY = "secure"; + private volatile NamingService namingService; private volatile NamingMaintainService namingMaintainService; private final NacosRegisterConfig nacosRegisterConfig; + private final RegisterServiceCommonConfig commonConfig; + /** * 构造方法 - * - * @param nacosRegisterConfig nacos配置信息 */ - public NacosServiceManager(NacosRegisterConfig nacosRegisterConfig) { - this.nacosRegisterConfig = nacosRegisterConfig; + public NacosServiceManager() { + nacosRegisterConfig = PluginConfigManager.getPluginConfig(NacosRegisterConfig.class); + commonConfig = PluginConfigManager.getPluginConfig(RegisterServiceCommonConfig.class); } /** @@ -116,9 +124,12 @@ public Instance buildNacosInstanceFromRegistration() { instance.setWeight(nacosRegisterConfig.getWeight()); instance.setClusterName(nacosRegisterConfig.getClusterName()); instance.setEnabled(nacosRegisterConfig.isInstanceEnabled()); - instance.setMetadata(RegisterContext.INSTANCE.getClientInfo().getMeta()); + Map metadata = RegisterContext.INSTANCE.getClientInfo().getMeta(); + if (!metadata.containsKey(ConfigConstants.SECURE)) { + metadata.put(ConfigConstants.SECURE, String.valueOf(commonConfig.isSecure())); + } + instance.setMetadata(metadata); instance.setEphemeral(nacosRegisterConfig.isEphemeral()); - nacosRegisterConfig.setMetadata(RegisterContext.INSTANCE.getClientInfo().getMeta()); return instance; } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/ServicecombServiceInstance.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/ServicecombServiceInstance.java index b3463a8152..4bafc26c7f 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/ServicecombServiceInstance.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/register/ServicecombServiceInstance.java @@ -17,6 +17,7 @@ package com.huawei.registry.service.register; +import com.huawei.registry.config.ConfigConstants; import com.huawei.registry.config.RegisterConfig; import com.huawei.registry.entity.MicroServiceInstance; import com.huawei.registry.utils.CommonUtils; @@ -100,4 +101,10 @@ public String getInstanceId() { public Map getMetadata() { return microserviceInstance.getProperties(); } + + @Override + public boolean isSecure() { + Map properties = microserviceInstance.getProperties(); + return Boolean.valueOf(properties.get(ConfigConstants.SECURE)); + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosClientTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosClientTest.java index a96c30855d..410037a4e1 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosClientTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosClientTest.java @@ -18,7 +18,9 @@ package com.huawei.registry.service.client; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.After; import org.junit.Assert; @@ -33,6 +35,7 @@ import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.ListView; import com.huawei.registry.config.NacosRegisterConfig; +import com.huawei.registry.config.RegisterServiceCommonConfig; import com.huawei.registry.context.RegisterContext; import com.huawei.registry.service.register.NacosServiceManager; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; @@ -49,9 +52,11 @@ public class NacosClientTest { private final NacosRegisterConfig registerConfig = new NacosRegisterConfig(); + private final RegisterServiceCommonConfig commonConfig = new RegisterServiceCommonConfig(); + private MockedStatic pluginConfigManagerMockedStatic; - private NacosServiceManager nacosServiceManager = new NacosServiceManager(registerConfig); + private NacosServiceManager nacosServiceManager; private NacosClient nacosClient; @@ -62,8 +67,15 @@ public void setUp() { .mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(NacosRegisterConfig.class)) .thenReturn(registerConfig); + commonConfig.setSecure(true); + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterServiceCommonConfig.class)) + .thenReturn(commonConfig); RegisterContext.INSTANCE.getClientInfo().setServiceId("test"); nacosClient = new NacosClient(); + nacosServiceManager = new NacosServiceManager(); + Map map = new HashMap<>(); + map.put("foo", "123"); + RegisterContext.INSTANCE.getClientInfo().setMeta(map); } @After diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosDiscoveryTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosDiscoveryTest.java index 2e8b5db014..caf8c85a6d 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosDiscoveryTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/NacosDiscoveryTest.java @@ -67,7 +67,7 @@ public void tearDown() { */ @Test public void testhostToServiceInstanceList() { - NacosServiceDiscovery discovery = new NacosServiceDiscovery(new NacosServiceManager(registerConfig)); + NacosServiceDiscovery discovery = new NacosServiceDiscovery(new NacosServiceManager()); List list = discovery.convertServiceInstanceList(instanceList, "test"); Assert.assertEquals(list.size(), instanceList.size()); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/register/NacosServiceManagerTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/register/NacosServiceManagerTest.java index 079440248a..b4db2bd483 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/register/NacosServiceManagerTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/register/NacosServiceManagerTest.java @@ -45,7 +45,7 @@ public class NacosServiceManagerTest { private final NacosRegisterConfig registerConfig = new NacosRegisterConfig(); private final RegisterServiceCommonConfig commonConfig = new RegisterServiceCommonConfig(); private MockedStatic pluginConfigManagerMockedStatic; - final NacosServiceManager nacosServiceManager = new NacosServiceManager(registerConfig); + private NacosServiceManager nacosServiceManager; @Before public void setUp() { @@ -55,12 +55,15 @@ public void setUp() { commonConfig.setAddress("127.0.0.1:8848"); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager .getPluginConfig(RegisterServiceCommonConfig.class)).thenReturn(commonConfig); + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager + .getPluginConfig(NacosRegisterConfig.class)).thenReturn(registerConfig); RegisterContext.INSTANCE.getClientInfo().setServiceName("test"); RegisterContext.INSTANCE.getClientInfo().setIp("127.0.0.1"); RegisterContext.INSTANCE.getClientInfo().setServiceId("test"); RegisterContext.INSTANCE.getClientInfo().setHost("localhost"); RegisterContext.INSTANCE.getClientInfo().setPort(8001); RegisterContext.INSTANCE.getClientInfo().setMeta(new HashMap<>()); + nacosServiceManager = new NacosServiceManager(); } @After