Search in sources :

Example 16 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class TestClientHttp method testIpPortManager.

@Test
public void testIpPortManager(@Mocked InstanceCacheManager instanceCacheManager) throws Exception {
    IpPortManager oManager = new IpPortManager(ServiceRegistryConfig.INSTANCE, instanceCacheManager);
    IpPort oIPPort = oManager.getNextAvailableAddress(new IpPort("", 33));
    Assert.assertEquals(oIPPort.getHostOrIp(), oManager.getAvailableAddress().getHostOrIp());
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) IpPortManager(org.apache.servicecomb.serviceregistry.client.IpPortManager) Test(org.junit.Test)

Example 17 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class TestRemoteServiceRegistry method testLifeCycle.

@Test
public void testLifeCycle(@Injectable ServiceRegistryConfig config, @Injectable MicroserviceDefinition definition, @Injectable ServiceRegistry registry) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            definition.getConfiguration();
            result = ConfigUtil.createLocalConfig();
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
            config.getHeartbeatInterval();
            result = 30;
            config.getInstancePullInterval();
            result = 30;
            config.isWatch();
            result = false;
        }
    };
    ServiceRegistry oldRegistry = RegistryUtils.getServiceRegistry();
    RegistryUtils.setServiceRegistry(registry);
    EventBus bus = new EventBus();
    RemoteServiceRegistry remote = new TestingRemoteServiceRegistry(bus, config, definition);
    remote.init();
    remote.run();
    // includes complete tasks
    Assert.assertTrue(2 <= remote.getTaskPool().getTaskCount());
    bus.post(new ShutdownEvent());
    remote.getTaskPool().schedule(new Runnable() {

        @Override
        public void run() {
        // TODO Auto-generated method stub
        }
    }, 0, TimeUnit.SECONDS);
    Assert.assertTrue(remote.getTaskPool().isShutdown());
    RegistryUtils.setServiceRegistry(oldRegistry);
}
Also used : Expectations(mockit.Expectations) ArrayList(java.util.ArrayList) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ServiceRegistry(org.apache.servicecomb.serviceregistry.ServiceRegistry) EventBus(com.google.common.eventbus.EventBus) ShutdownEvent(org.apache.servicecomb.serviceregistry.task.event.ShutdownEvent) Test(org.junit.Test)

Example 18 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.

the class DefaultMonitorDataPublisher method doSend.

private void doSend(String endpoint, String jsonData, String url, IpPort host, int times) {
    clientMgr.findThreadBindClientPool().runOnContext(client -> {
        client.request(HttpMethod.POST, host.getPort(), host.getHostOrIp(), url).compose(request -> {
            request.headers().add("environment", RegistryUtils.getMicroservice().getEnvironment());
            request.setTimeout(MonitorConstant.getInterval() / MonitorConstant.MAX_RETRY_TIMES);
            try {
                SignRequest signReq = SignUtil.createSignRequest(request.getMethod().toString(), endpoint + url, new HashMap<>(), IOUtils.toInputStream(jsonData, "UTF-8"));
                SignUtil.getAuthHeaderProviders().forEach(authHeaderProvider -> {
                    request.headers().addAll(authHeaderProvider.getSignAuthHeaders(signReq));
                });
            } catch (Exception e) {
                LOGGER.error("sign request error!", e);
            }
            return request.send(jsonData).compose(rsp -> {
                if (rsp.statusCode() != HttpResponseStatus.OK.code()) {
                    addressManager.recordSuccessState(endpoint);
                    if (times < MonitorConstant.MAX_RETRY_TIMES && rsp.statusCode() == HttpResponseStatus.BAD_GATEWAY.code()) {
                        doSend(endpoint, jsonData, url, host, times + 1);
                        return Future.succeededFuture();
                    }
                    return rsp.body().compose(buffer -> {
                        LOGGER.warn("Send data to url {} failed and status line is {}", url, rsp.statusCode());
                        LOGGER.warn("message: {}", buffer);
                        return Future.succeededFuture();
                    });
                } else {
                    addressManager.recordSuccessState(endpoint);
                    EventManager.post(new MonitorSuccEvent());
                }
                return Future.succeededFuture();
            }).onFailure(failure -> {
                EventManager.post(new MonitorFailEvent("send monitor data fail."));
                addressManager.recordFailState(endpoint);
                LOGGER.warn("Send monitor data to {} failed , {}", endpoint, failure);
            });
        });
    });
}
Also used : Json(io.vertx.core.json.Json) RegistryUtils(org.apache.servicecomb.serviceregistry.RegistryUtils) MonitorConstant(org.apache.servicecomb.huaweicloud.dashboard.monitor.data.MonitorConstant) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ArrayList(java.util.ArrayList) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) VertxUtils(org.apache.servicecomb.foundation.vertx.VertxUtils) SSLCustom(org.apache.servicecomb.foundation.ssl.SSLCustom) SSLOption(org.apache.servicecomb.foundation.ssl.SSLOption) VertxTLSBuilder(org.apache.servicecomb.foundation.vertx.VertxTLSBuilder) AddressResolverConfig(org.apache.servicecomb.foundation.vertx.AddressResolverConfig) SSLOptionFactory(org.apache.servicecomb.foundation.ssl.SSLOptionFactory) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Deployment(org.apache.servicecomb.deployment.Deployment) SignRequest(org.apache.servicecomb.foundation.auth.SignRequest) NetUtils(org.apache.servicecomb.foundation.common.net.NetUtils) MonitorDataPublisher(org.apache.servicecomb.huaweicloud.dashboard.monitor.model.MonitorDataPublisher) ProxyOptions(io.vertx.core.net.ProxyOptions) Logger(org.slf4j.Logger) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) MonitorFailEvent(org.apache.servicecomb.huaweicloud.dashboard.monitor.event.MonitorFailEvent) ClientVerticle(org.apache.servicecomb.foundation.vertx.client.ClientVerticle) Vertx(io.vertx.core.Vertx) MonitorSuccEvent(org.apache.servicecomb.huaweicloud.dashboard.monitor.event.MonitorSuccEvent) VertxOptions(io.vertx.core.VertxOptions) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Future(io.vertx.core.Future) MonitorDaraProvider(org.apache.servicecomb.huaweicloud.dashboard.monitor.model.MonitorDaraProvider) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) HttpClientPoolFactory(org.apache.servicecomb.foundation.vertx.client.http.HttpClientPoolFactory) ClientPoolManager(org.apache.servicecomb.foundation.vertx.client.ClientPoolManager) DeploymentOptions(io.vertx.core.DeploymentOptions) HttpMethod(io.vertx.core.http.HttpMethod) SystemBootstrapInfo(org.apache.servicecomb.deployment.SystemBootstrapInfo) MonitorSuccEvent(org.apache.servicecomb.huaweicloud.dashboard.monitor.event.MonitorSuccEvent) SignRequest(org.apache.servicecomb.foundation.auth.SignRequest) MonitorFailEvent(org.apache.servicecomb.huaweicloud.dashboard.monitor.event.MonitorFailEvent)

Example 19 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.

the class DefaultMonitorDataPublisher method publish.

@Override
public void publish(MonitorDaraProvider provider) {
    Object data = provider.getData();
    if (data == null) {
        return;
    }
    String endpoint = addressManager.address();
    if (endpoint == null) {
        return;
    }
    String jasonData = Json.encode(data);
    String url = provider.getURL();
    IpPort host = NetUtils.parseIpPortFromURI(endpoint);
    doSend(endpoint, jasonData, url, host, 0);
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort)

Example 20 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.

the class ServiceRegistryConfigCustomizer method addressListFromConfiguration.

public ServiceRegistryConfigCustomizer addressListFromConfiguration(String configuration) {
    String address = DynamicPropertyFactory.getInstance().getStringProperty(configuration, null).get();
    if (address == null) {
        throw new IllegalStateException("service center address is required.");
    }
    String[] urls = address.split(",");
    List<String> uriList = Arrays.asList(urls);
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    uriList.forEach(anUriList -> {
        try {
            URI uri = new URI(anUriList.trim());
            if ("https".equals(uri.getScheme())) {
                this.original.setSsl(true);
            }
            ipPortList.add(NetUtils.parseIpPort(uri));
        } catch (Exception e) {
            throw new IllegalStateException("service center address is required.", e);
        }
    });
    this.original.setIpPort(ipPortList);
    return this;
}
Also used : ArrayList(java.util.ArrayList) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) URI(java.net.URI)

Aggregations

IpPort (org.apache.servicecomb.foundation.common.net.IpPort)69 ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)36 CountDownLatch (java.util.concurrent.CountDownLatch)35 IOException (java.io.IOException)19 ExecutionException (java.util.concurrent.ExecutionException)19 Holder (javax.xml.ws.Holder)15 Test (org.junit.Test)14 HttpClientResponse (io.vertx.core.http.HttpClientResponse)12 ArrayList (java.util.ArrayList)10 Handler (io.vertx.core.Handler)6 URI (java.net.URI)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Expectations (mockit.Expectations)6 MockUp (mockit.MockUp)6 Future (io.vertx.core.Future)5 HttpClientRequest (io.vertx.core.http.HttpClientRequest)5 HttpMethod (io.vertx.core.http.HttpMethod)5 SignRequest (org.apache.servicecomb.foundation.auth.SignRequest)4 HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)4