Skip to content

Commit

Permalink
Merge pull request #2287 from rushsky518/sync_content
Browse files Browse the repository at this point in the history
HttpCLient uses ByteArrayEntity when sync data
  • Loading branch information
KomachiSion authored Jun 10, 2020
2 parents 7f7ee0d + 9c5bead commit f49eba3
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public ResponseEntity get(@RequestBody String body) throws Exception {
datumMap.put(key, datum);
}

String content = new String(serializer.serialize(datumMap), StandardCharsets.UTF_8);
byte[] content = serializer.serialize(datumMap);
return ResponseEntity.ok(content);
}

@GetMapping("/datums")
public ResponseEntity getAllDatums() {
String content = new String(serializer.serialize(dataStore.getDataMap()), StandardCharsets.UTF_8);
byte[] content = serializer.serialize(dataStore.getDataMap());
return ResponseEntity.ok(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -318,18 +319,16 @@ public static void asyncHttpGetLarge(String url, Map<String, String> headers, by

public static HttpResult httpPutLarge(String url, Map<String, String> headers, byte[] content) {
try {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);

HttpClientBuilder builder = HttpClients.custom()
.setUserAgent(UtilsAndCommons.SERVER_VERSION)
.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
CloseableHttpClient httpClient = builder.build();
HttpPut httpPut = new HttpPut(url);

HttpPut httpPut = new HttpPut(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}

httpPut.setEntity(new StringEntity(new String(content, StandardCharsets.UTF_8), ContentType.create("application/json", StandardCharsets.UTF_8)));
httpPut.setEntity(new ByteArrayEntity(content, ContentType.APPLICATION_JSON));

HttpResponse response = httpClient.execute(httpPut);
HttpEntity entity = response.getEntity();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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.alibaba.nacos.test.naming;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.naming.NamingApp;
import com.alibaba.nacos.naming.cluster.transport.Serializer;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.ephemeral.distro.DataStore;
import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.Instances;
import com.alibaba.nacos.naming.misc.NamingProxy;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NamingProxy_ITCase {
@LocalServerPort
private int port;
@Autowired
private Serializer serializer;
private final DataStore dataStore = new DataStore();
private NamingService namingService;
private final String namespaceId = "dev";
private final String serviceName = "biz";
private final String groupName = "DEFAULT_GROUP";

@Before
public void init() throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.NAMESPACE, namespaceId);
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:" + port);
namingService = NamingFactory.createNamingService(properties);
}

@Test
public void testSyncData() throws NacosException, InterruptedException {
// write data to DataStore
String groupedName = NamingUtils.getGroupedName(serviceName, groupName);
Instances instances = new Instances();
Instance instance = new Instance();
instance.setIp("1.2.3.4");
instance.setPort(8888);
instance.setEphemeral(true);
instance.setServiceName(groupedName);
List<Instance> instanceList = new ArrayList<Instance>(1);
instanceList.add(instance);
instances.setInstanceList(instanceList);
String key = KeyBuilder.buildInstanceListKey(namespaceId, instance.getServiceName(), true);

Datum<Instances> datum = new Datum<>();
datum.value = instances;
datum.key = key;
datum.timestamp.incrementAndGet();
this.dataStore.put(key, datum);

// sync data to server
Map<String, Datum> dataMap = dataStore.getDataMap();
byte[] content = serializer.serialize(dataMap);
boolean result = NamingProxy.syncData(content, "localhost:" + port);
if (!result) {
Assert.fail("NamingProxy.syncData error");
}

// query instance by api
List<com.alibaba.nacos.api.naming.pojo.Instance> allInstances = namingService.getAllInstances(serviceName, false);
for (int i = 0; i < 3 && allInstances.isEmpty(); i++) {
// wait for async op
TimeUnit.SECONDS.sleep(100);
allInstances = namingService.getAllInstances(serviceName, false);
}
if (allInstances.isEmpty()) {
Assert.fail("instance is empty");
}

com.alibaba.nacos.api.naming.pojo.Instance dst = allInstances.get(0);
Assert.assertEquals(instance.getIp(), dst.getIp());
Assert.assertEquals(instance.getPort(), dst.getPort());
Assert.assertEquals(instance.getServiceName(), dst.getServiceName());
}

}

0 comments on commit f49eba3

Please sign in to comment.