Java :: Berlioz Documentation

Java

Berlioz Java SDK is depends on SpringBoot Framework.

Setup

In pom.xml file add following dependency:

<dependency>
    <groupId>com.berlioz</groupId>
    <artifactId>berlioz-sdk</artifactId>
    <version>1.1.20-SNAPSHOT</version>
</dependency>

and repository:

<repository>
    <id>apache.snapshots</id>
    <name>Apache Development Snapshot Repository</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    <releases>
        <enabled>false</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

Initial Configuration

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer
{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new com.berlioz.spring.Handler());
    }
}

Send HTTP Request to Service

Berlioz SDK provides wrapper to RestTemplate preserving standard interface to send HTTP requests to other services.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer
{
    @Bean(name = "inventory")
    public RestTemplate restTemplateApp() {
        return new com.berlioz.http.RestTemplateBuilder()
                .service("inventory")
                .build();
    }
}
@RestController
public class HelloController {

    @Autowired
    @Qualifier("inventory")
    RestTemplate inventoryRestTemplate;

    @RequestMapping("/")
    public String handler() {
        String response = inventoryRestTemplate.getForObject("/items", String.class);
    }
}

Send HTTP Request to Cluster

The only difference is in RestTemplateBuilder. Just specify cluster instead of service.

@Bean(name = "inventory")
public RestTemplate restTemplateApp() {
    return new com.berlioz.http.RestTemplateBuilder()
            .cluster("inventory")
            .build();
}