This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new f1e649ece [SCB-2663]Support to update microservice properties to the
registration center (#3261)
f1e649ece is described below
commit f1e649ece2a60d2f66f3bd4d93e683bd7a2562a1
Author: Denghuang Wei <[email protected]>
AuthorDate: Tue Aug 9 20:01:01 2022 +0800
[SCB-2663]Support to update microservice properties to the registration
center (#3261)
---
.../service/center/client/ServiceCenterClient.java | 22 +++++++++++++++
.../center/client/ServiceCenterOperation.java | 9 ++++++
.../center/client/ServiceCenterRegistration.java | 13 +++++++++
.../client/model/UpdatePropertiesRequest.java | 32 ++++++++++++++++++++++
.../center/client/ServiceCenterClientTest.java | 19 +++++++++++++
.../task/MicroserviceRegisterTask.java | 19 +++++++++++++
.../task/TestMicroserviceRegisterTask.java | 4 +--
7 files changed, 116 insertions(+), 2 deletions(-)
diff --git
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterClient.java
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterClient.java
index 9f0dfb926..849d5181d 100755
---
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterClient.java
+++
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterClient.java
@@ -55,6 +55,7 @@ import
org.apache.servicecomb.service.center.client.model.RbacTokenResponse;
import
org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse;
import
org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceResponse;
import org.apache.servicecomb.service.center.client.model.SchemaInfo;
+import
org.apache.servicecomb.service.center.client.model.UpdatePropertiesRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -527,4 +528,25 @@ public class ServiceCenterClient implements
ServiceCenterOperation {
"query token failed", e);
}
}
+
+ @Override
+ public boolean updateMicroserviceProperties(String serviceId, Map<String,
String> serviceProperties) {
+ try {
+ UpdatePropertiesRequest request = new UpdatePropertiesRequest();
+ request.setProperties(serviceProperties);
+ HttpResponse response = httpClient.putHttpRequest(
+ "/registry/microservices/" + serviceId + "/properties", null,
HttpUtils.serialize(request));
+ if (response.getStatusCode() == HttpStatus.SC_OK) {
+ return true;
+ }
+ sendUnAuthorizedEvent(response);
+ throw new OperationException(
+ "update service instance status fails, statusCode = " +
response.getStatusCode() + "; message = " + response
+ .getMessage()
+ + "; content = " + response.getContent());
+ } catch (IOException e) {
+ throw new OperationException(
+ "update service instance status fails", e);
+ }
+ }
}
diff --git
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterOperation.java
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterOperation.java
index 9c28ac36a..5521a7322 100644
---
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterOperation.java
+++
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterOperation.java
@@ -18,6 +18,7 @@
package org.apache.servicecomb.service.center.client;
import java.util.List;
+import java.util.Map;
import
org.apache.servicecomb.service.center.client.exception.OperationException;
import org.apache.servicecomb.service.center.client.model.CreateSchemaRequest;
@@ -184,4 +185,12 @@ public interface ServiceCenterOperation {
* @throws OperationException If some problems happened to contact service
center or non http 200 returned.
*/
RbacTokenResponse queryToken(RbacTokenRequest request);
+
+ /**
+ * Update properties of microservice
+ *
+ * @return if update is successful
+ * @throws OperationException If some problems happened to contact service
center or non http 200 returned.
+ */
+ boolean updateMicroserviceProperties(String microserviceId, Map<String,
String> serviceProperties);
}
diff --git
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
index 38de99e91..bb9021339 100644
---
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
+++
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/ServiceCenterRegistration.java
@@ -18,6 +18,7 @@
package org.apache.servicecomb.service.center.client;
import java.util.List;
+import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.servicecomb.http.client.task.AbstractTask;
@@ -131,6 +132,18 @@ public class ServiceCenterRegistration extends
AbstractTask {
startTask(new RegisterSchemaTask(0));
} else {
Microservice newMicroservice =
serviceCenterClient.getMicroserviceByServiceId(serviceResponse.getServiceId());
+
+ Map<String, String> propertiesTemp = microservice.getProperties();
+ microservice.setProperties(newMicroservice.getProperties());
+ microservice.getProperties().putAll(propertiesTemp);
+ if
(serviceCenterClient.updateMicroserviceProperties(serviceResponse.getServiceId(),
microservice.getProperties())) {
+ LOGGER.info("microservice is already registered. Update
microservice properties successfully. properties=[{}]",
+ microservice.getProperties());
+ } else {
+ LOGGER.error("microservice is already registered. Update
microservice properties failed. properties=[{}]",
+ microservice.getProperties());
+ }
+
microservice.setServiceId(serviceResponse.getServiceId());
microserviceInstance.setServiceId(serviceResponse.getServiceId());
microserviceInstance.setMicroservice(microservice);
diff --git
a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/model/UpdatePropertiesRequest.java
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/model/UpdatePropertiesRequest.java
new file mode 100644
index 000000000..b10586208
--- /dev/null
+++
b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/model/UpdatePropertiesRequest.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.servicecomb.service.center.client.model;
+
+import java.util.Map;
+
+public class UpdatePropertiesRequest {
+ private Map<String, String> properties;
+
+ public Map<String, String> getProperties() {
+ return properties;
+ }
+
+ public void setProperties(Map<String, String> properties) {
+ this.properties = properties;
+ }
+}
diff --git
a/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/ServiceCenterClientTest.java
b/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/ServiceCenterClientTest.java
index 0c2abfbd1..0a2a96b52 100755
---
a/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/ServiceCenterClientTest.java
+++
b/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/ServiceCenterClientTest.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.service.center.client;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import org.apache.servicecomb.http.client.common.HttpResponse;
@@ -489,4 +490,22 @@ public class ServiceCenterClientTest {
Assertions.assertTrue(result);
}
+
+ @Test
+ public void testUpdateMicroserviceProperties() throws IOException {
+ ServiceCenterRawClient serviceCenterRawClient =
Mockito.mock(ServiceCenterRawClient.class);
+
+ HttpResponse httpResponse = new HttpResponse();
+ httpResponse.setStatusCode(200);
+ httpResponse.setMessage("ok");
+
+ Mockito.when(serviceCenterRawClient.putHttpRequest(Mockito.any(),
Mockito.any(), Mockito.any()))
+ .thenReturn(httpResponse);
+
+ ServiceCenterClient serviceCenterClient = new
ServiceCenterClient(serviceCenterRawClient);
+ boolean result = serviceCenterClient
+ .updateMicroserviceProperties("111", new HashMap<String,
String>());
+
+ Assertions.assertTrue(result);
+ }
}
diff --git
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
index 38eacace4..8193526bf 100644
---
a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
+++
b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/task/MicroserviceRegisterTask.java
@@ -86,6 +86,25 @@ public class MicroserviceRegisterTask extends
AbstractRegisterTask {
if (!StringUtils.isEmpty(serviceId)) {
// This microservice has been registered, so we just use the serviceId
gotten from service center
microservice.setServiceId(serviceId);
+
+ // Need to update microservice properties if we have modified or added
properties of microservices.
+ Microservice microserviceTemp = new Microservice();
+
EnvAdapterManager.INSTANCE.processMicroserviceWithAdapters(microserviceTemp);
+ if (!microserviceTemp.getProperties().isEmpty()) {
+ Map<String, String> propertiesTemp = microserviceTemp.getProperties();
+ Microservice srClientMicroservice =
srClient.getMicroservice(serviceId);
+ if (srClientMicroservice != null) {
+ microserviceTemp.setProperties(srClientMicroservice.getProperties());
+ microserviceTemp.getProperties().putAll(propertiesTemp);
+ }
+ if (srClient.updateMicroserviceProperties(serviceId,
microserviceTemp.getProperties())) {
+ LOGGER.info("microservice is already registered. Update microservice
properties successfully. properties=[{}]",
+ microserviceTemp.getProperties());
+ } else {
+ LOGGER.error("microservice is already registered. Update
microservice properties failed. properties=[{}]",
+ microserviceTemp.getProperties());
+ }
+ }
LOGGER.info(
"Microservice exists in service center, no need to register. id=[{}]
appId=[{}], name=[{}], version=[{}], env=[{}]",
serviceId,
diff --git
a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
index 4e37d05c0..d1cabe096 100644
---
a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
+++
b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/task/TestMicroserviceRegisterTask.java
@@ -682,7 +682,7 @@ public class TestMicroserviceRegisterTask {
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
- " x-java-class:
\"org.apache.servicecomb.demo.validator.Student\"]",
events.get(4).getMessage());
+ " x-java-class:
\"org.apache.servicecomb.demo.validator.Student\"]",
events.get(5).getMessage());
Assertions.assertEquals("The difference in local schema:\n" +
"[type: \"string\"\n" +
@@ -698,7 +698,7 @@ public class TestMicroserviceRegisterTask {
" type: \"integer\"\n" +
" format: \"int32\"\n" +
" maximum: 20\n" +
- " x-java-class:
\"org.apache.servicecomb.demo.validator.Student\"]",
events.get(5).getMessage());
+ " x-java-class:
\"org.apache.servicecomb.demo.validator.Student\"]",
events.get(6).getMessage());
}
Assertions.assertTrue(isIllegalException);