org.apache.camel.impl.cloud.DefaultServiceDefinition Java Examples

The following examples show how to use org.apache.camel.impl.cloud.DefaultServiceDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelCloudServiceFilterAutoConfiguration.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
private CamelCloudServiceFilter createServiceFilter(CamelCloudConfigurationProperties.ServiceFilterConfiguration configuration) {
    BlacklistServiceFilter blacklist = new BlacklistServiceFilter();

    Map<String, List<String>> services = configuration.getBlacklist();
    for (Map.Entry<String, List<String>> entry : services.entrySet()) {
        for (String part : entry.getValue()) {
            String host = StringHelper.before(part, ":");
            String port = StringHelper.after(part, ":");

            if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) {
                blacklist.addServer(
                    DefaultServiceDefinition.builder()
                        .withName(entry.getKey())
                        .withHost(host)
                        .withPort(Integer.parseInt(port))
                        .build()
                );
            }
        }
    }

    return new CamelCloudServiceFilter(Arrays.asList(new HealthyServiceFilter(), blacklist));
}
 
Example #2
Source File: DefaultServiceLoadBalancer.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
protected ServiceDefinition convertServiceInstanceToServiceDefinition(ServiceInstance instance) {
    return new DefaultServiceDefinition(
        instance.getServiceId(),
        instance.getHost(),
        instance.getPort(),
        instance.getMetadata()
    );
}
 
Example #3
Source File: CamelSpringCloudServiceDiscovery.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public List<ServiceDefinition> getServices(String name) {
    return discoveryClient.getInstances(name).stream()
        .map(
            si -> {
                return DefaultServiceDefinition.builder()
                    .withName(si.getServiceId())
                    .withHost(si.getHost())
                    .withPort(si.getPort())
                    .withId(name)
                    .withMeta(si.getMetadata())
                    .build();
            }
        ).collect(Collectors.toList());
}
 
Example #4
Source File: ConsulServerToServiceDefinition.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceDefinition convert(ConsulServer source) {
    return new DefaultServiceDefinition(
        source.getId(),
        source.getHost(),
        source.getPort(),
        source.getMetadata()
    );
}
 
Example #5
Source File: ZookeeperServerToServiceDefinition.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceDefinition convert(ZookeeperServer source) {
    return new DefaultServiceDefinition(
        source.getId(),
        source.getHost(),
        source.getPort(),
        source.getInstance().getPayload().getMetadata()
    );
}
 
Example #6
Source File: ZooKeeperServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() throws Exception {
    final int zkPort =  AvailablePortFinder.getNextAvailable();
    final File zkDir =  temporaryFolder.newFolder();

    final TestingServer zkServer = new TestingServer(zkPort, zkDir);
    zkServer.start();

    final ZooKeeperTestClient zkClient = new ZooKeeperTestClient("localhost:" + zkPort);
    zkClient.start();

    try {
        new ApplicationContextRunner()
            .withUserConfiguration(TestConfiguration.class)
            .withPropertyValues(
                "debug=false",
                "spring.main.banner-mode=OFF",
                "spring.application.name=" + UUID.randomUUID().toString(),
                "camel.component.zookeeper.service-registry.enabled=true",
                "camel.component.zookeeper.service-registry.nodes=localhost:" + zkPort,
                "camel.component.zookeeper.service-registry.id=" + UUID.randomUUID().toString(),
                "camel.component.zookeeper.service-registry.base-path=" + SERVICE_PATH,
                "camel.component.zookeeper.service-registry.service-host=localhost")
            .run(
                context -> {
                    assertThat(context).hasSingleBean(CamelContext.class);
                    assertThat(context).hasSingleBean(ServiceRegistry.class);

                    final CamelContext camelContext = context.getBean(CamelContext.class);
                    final ServiceRegistry serviceRegistry = camelContext.hasService(ServiceRegistry.class);

                    assertThat(serviceRegistry).isNotNull();

                    serviceRegistry.register(
                        DefaultServiceDefinition.builder()
                            .withHost(SERVICE_HOST)
                            .withPort(SERVICE_PORT)
                            .withName(SERVICE_NAME)
                            .withId(SERVICE_ID)
                            .build()
                    );

                    final Collection<ServiceInstance<ZooKeeperServiceRegistry.MetaData>> services = zkClient.discovery().queryForInstances(SERVICE_NAME);

                    assertThat(services).hasSize(1);
                    assertThat(services).first().hasFieldOrPropertyWithValue("id", SERVICE_ID);
                    assertThat(services).first().hasFieldOrPropertyWithValue("name", SERVICE_NAME);
                    assertThat(services).first().hasFieldOrPropertyWithValue("address", SERVICE_HOST);
                    assertThat(services).first().hasFieldOrPropertyWithValue("port", SERVICE_PORT);
                }
            );
    } finally {
        zkClient.stop();
        zkServer.stop();
    }
}
 
Example #7
Source File: ConsulServiceRegistryIT.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() {
    final String consulUrl = String.format("http://%s:%d", container.getContainerIpAddress(), container.getMappedPort(Consul.DEFAULT_HTTP_PORT));

    new ApplicationContextRunner()
        .withUserConfiguration(TestConfiguration.class)
        .withPropertyValues(
            "debug=false",
            "spring.main.banner-mode=OFF",
            "spring.application.name=" + UUID.randomUUID().toString(),
            "camel.component.consul.service-registry.enabled=true",
            "camel.component.consul.service-registry.url=" + consulUrl,
            "camel.component.consul.service-registry.id=" + UUID.randomUUID().toString(),
            "camel.component.consul.service-registry.service-host=localhost")
        .run(
            context -> {
                assertThat(context).hasSingleBean(CamelContext.class);
                assertThat(context).hasSingleBean(ServiceRegistry.class);

                final CamelContext camelContext =  context.getBean(CamelContext.class);
                final ServiceRegistry serviceRegistry = camelContext.hasService(ServiceRegistry.class);

                assertThat(serviceRegistry).isNotNull();

                serviceRegistry.register(
                    DefaultServiceDefinition.builder()
                        .withHost(SERVICE_HOST)
                        .withPort(SERVICE_PORT)
                        .withName(SERVICE_NAME)
                        .withId(SERVICE_ID)
                        .build()
                );

                final Consul client = Consul.builder().withUrl(consulUrl).build();
                final List<CatalogService> services = client.catalogClient().getService(SERVICE_NAME).getResponse();

                assertThat(services).hasSize(1);
                assertThat(services).first().hasFieldOrPropertyWithValue("serviceId", SERVICE_ID);
                assertThat(services).first().hasFieldOrPropertyWithValue("serviceName", SERVICE_NAME);
                assertThat(services).first().hasFieldOrPropertyWithValue("serviceAddress", SERVICE_HOST);
                assertThat(services).first().hasFieldOrPropertyWithValue("servicePort", SERVICE_PORT);
            }
        );
}
 
Example #8
Source File: CamelSpringCloudServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() {
    new ApplicationContextRunner()
        .withConfiguration(
            AutoConfigurations.of(
                CamelAutoConfiguration.class,
                CamelCloudAutoConfiguration.class,
                CamelSpringCloudServiceRegistryAutoConfiguration.class
            ))
        .withUserConfiguration(
            TestConfiguration.class)
        .withPropertyValues(
            "spring.main.banner-mode=off",
            "ribbon.eureka.enabled=false",
            "ribbon.enabled=false")
        .run(
            context -> {
                CamelSpringCloudServiceRegistry camelRgistry = context.getBean(CamelSpringCloudServiceRegistry.class);

                final String serviceName = "my-.service";
                final String serviceId = UUID.randomUUID().toString();
                final int port = ThreadLocalRandom.current().nextInt();

                camelRgistry.register(
                    DefaultServiceDefinition.builder()
                        .withHost("localhost")
                        .withPort(port)
                        .withName(serviceName)
                        .withId(serviceId)
                        .build()
                );

                MyServiceRegistry cloudRegistry = camelRgistry.getNativeServiceRegistry(MyServiceRegistry.class);

                assertThat(cloudRegistry.registrations).hasSize(1);
                assertThat(cloudRegistry.registrations.get(0)).hasFieldOrPropertyWithValue("serviceId", serviceName);
                assertThat(cloudRegistry.registrations.get(0)).hasFieldOrPropertyWithValue("host", "localhost");
                assertThat(cloudRegistry.registrations.get(0)).hasFieldOrPropertyWithValue("port", port);

            }
        );
}
 
Example #9
Source File: CamelCloudConsulServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() throws Exception {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfiguration.class)
        .web(WebApplicationType.NONE)
        .run(
            "--debug=false",
            "--spring.main.banner-mode=OFF",
            "--spring.application.name=" + UUID.randomUUID().toString(),
            "--ribbon.enabled=false",
            "--ribbon.eureka.enabled=false",
            "--management.endpoint.enabled=false",
            "--spring.cloud.consul.enabled=true",
            "--spring.cloud.consul.host=" + container.getContainerIpAddress(),
            "--spring.cloud.consul.port=" + container.getMappedPort(8500),
            "--spring.cloud.consul.config.enabled=false",
            "--spring.cloud.consul.discovery.enabled=true",
            "--spring.cloud.service-registry.auto-registration.enabled=false",
            "--camel.cloud.service-registry.service-host=localhost",
            "--spring.main.allow-bean-definition-overriding=true"
        );
    // TODO: Remove --spring.main.allow-bean-definition-overriding=true when new version of spring-cloud
    //  is released that supports Spring Boot 2.1 more properly

    try {
        final ConsulClient client = context.getBean(ConsulClient.class);
        final ServiceRegistry registry = context.getBean(ServiceRegistry.class);

        registry.register(
            DefaultServiceDefinition.builder()
                .withHost(SERVICE_HOST)
                .withPort(SERVICE_PORT)
                .withName(SERVICE_NAME)
                .withId(SERVICE_ID)
                .build()
        );

        List<CatalogService> services = client.getCatalogService(SERVICE_NAME, QueryParams.DEFAULT).getValue();
        
        assertThat(services).hasSize(1);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceId", SERVICE_ID);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceName", SERVICE_NAME);
        assertThat(services).first().hasFieldOrPropertyWithValue("serviceAddress", SERVICE_HOST);
        assertThat(services).first().hasFieldOrPropertyWithValue("servicePort", SERVICE_PORT);

    } finally {
        context.close();
    }
}
 
Example #10
Source File: CamelCloudZookeeperServiceRegistryTest.java    From camel-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceRegistry() throws Exception {
    final ZookeeperServer server = new ZookeeperServer(temporaryFolder.newFolder(testName.getMethodName()));

    ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfiguration.class)
        .web(WebApplicationType.NONE)
        .run(
            "--debug=false",
            "--spring.main.banner-mode=OFF",
            "--spring.application.name=" + UUID.randomUUID().toString(),
            "--ribbon.enabled=false",
            "--ribbon.eureka.enabled=false",
            "--management.endpoint.enabled=false",
            "--spring.cloud.zookeeper.enabled=true",
            "--spring.cloud.zookeeper.connect-string=" + server.connectString(),
            "--spring.cloud.zookeeper.config.enabled=false",
            "--spring.cloud.zookeeper.discovery.enabled=true",
            "--spring.cloud.service-registry.auto-registration.enabled=false",
            "--camel.cloud.service-registry.service-host=" + SERVICE_HOST
        );

    try {
        final ServiceDiscovery client = context.getBean(ServiceDiscovery.class);
        final ServiceRegistry registry = context.getBean(ServiceRegistry.class);

        registry.register(
            DefaultServiceDefinition.builder()
                .withHost(SERVICE_HOST)
                .withPort(SERVICE_PORT)
                .withName(SERVICE_NAME)
                .withId(SERVICE_ID)
                .build()
        );

        Collection<ServiceInstance<ZookeeperInstance>> services = client.queryForInstances(SERVICE_NAME);
        
        assertThat(services).hasSize(1);
        assertThat(services).first().hasFieldOrPropertyWithValue("address", SERVICE_HOST);
        assertThat(services).first().hasFieldOrPropertyWithValue("port", SERVICE_PORT);
        assertThat(services).first().extracting("payload").hasFieldOrPropertyWithValue("id", SERVICE_ID);
        assertThat(services).first().extracting("payload").hasFieldOrPropertyWithValue("name", SERVICE_NAME);

    } finally {
        // shutdown spring context
        context.close();

        // shutdown zookeeper
        server.shutdown();
    }
}
 
Example #11
Source File: RibbonIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceCall() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            StaticServiceDiscovery servers = new StaticServiceDiscovery();
            /* It would be more natural to test against two services running on two distinct ports or
             * at least on two distinct context paths, but none of those is possible because of
             * https://github.com/wildfly-extras/wildfly-camel/issues/2129
             * and https://issues.apache.org/jira/browse/CAMEL-11882 */
            servers.addServer(new DefaultServiceDefinition("my-service", "localhost", 8080));

            RibbonConfiguration configuration = new RibbonConfiguration();
            RibbonServiceLoadBalancer loadBalancer = new RibbonServiceLoadBalancer(configuration);

            from("direct:start") //
                    .serviceCall() //
                    .name("my-service") //
                    .uri("undertow:http://my-service/my-app")//
                    .loadBalancer(loadBalancer) //
                    .serviceDiscovery(servers) //
                    .end() //
                    .to("mock:result");

            from("undertow:http://localhost:8080/my-app") //
                    .to("mock:8080") //
                    .transform().constant("8080");
        }
    });

    MockEndpoint mockEndpoint8080 = camelctx.getEndpoint("mock:8080", MockEndpoint.class);
    mockEndpoint8080.expectedMessageCount(2);
    MockEndpoint mockEndpointResult = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpointResult.expectedMessageCount(2);

    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();

        Thread.sleep(20000);
        String out = template.requestBody("direct:start", null, String.class);
        String out2 = template.requestBody("direct:start", null, String.class);
        Assert.assertEquals("8080", out);
        Assert.assertEquals("8080", out2);

        mockEndpoint8080.assertIsSatisfied();
        mockEndpointResult.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}