Enterprise Java

Introduction to Spring Cloud – Config (Part I)

1. Overview

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state).

It helps manage the complexity involved in building the distributed system.

2. Microservices

Microservices is a software development architectural style, that decomposes the application into a collection of loosely coupled services.

It improves modularity, thus making the application easier to develop, test & deploy.

It also makes the development process more efficient by parallelizing small teams to work on different services.

There are also various difficulties regarding communication between services, managing configurations, etc in a microservice architecture.

One should go through the Twelve-Factor App Manifesto to solve many of the problems arising with a Microservice architecture.

3. Spring Cloud Config

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system.

It has two components, the Config Server & the Config Client.

The Config Server is a central place to manage external properties for applications across all environments. We could also version the configuration files using Git. It exposes REST API’s for clients to connect and get the required configuration. We can also leverage Spring Profiles to manage different configuration files for different profiles (environments).

3. Dependencies

We’ll use Gradle to build our project. I recommend using Spring Initializr for bootstrapping your project.

We’ll use:

  • Spring Boot 2
  • Spring Webflux
  • Spring Reactive Data MongoDB
  • Spring Security Reactive Webflux
  • Lombok

Not all the Spring libraries have a stable release yet.

Lombok is used to reduce boilerplate code for models and POJOs. It can generate setters/getters, default constructors, toString, etc. methods automatically.

  
buildscript {
	ext {
		springBootVersion = '2.0.0.M2'
	}
...
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
	compile('org.springframework.boot:spring-boot-starter-webflux')
	compile('org.springframework.security:spring-security-core')
	compile('org.springframework.security:spring-security-config')
	compile('org.springframework.security:spring-security-webflux')
	compileOnly('org.projectlombok:lombok')
...
}

4. Auto-Configuration

We’ll leave Spring Boot to automatically configure our application based on the dependencies added.

  
@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFluxSecurity
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

For using non-default values in our application configuration, we can specify them as properties and Spring Boot will automatically use them to create beans.

  
spring.data.mongodb.database=demo

All beans necessary for MongoDB, Web, and Security will be automatically created.

5. Database

We’ll be using MongoDB in our example and a simple POJO. A PersonRepository bean will be created automatically.

  
@Data
@NoArgsConstructor
@Document
public class Person {
    @Id 
    private String id;
    private String name;
}

public interface PersonRespository extends ReactiveMongoRepository<Person, String> {
    Flux<Person> findByName(String name);
}

6. Web API

We’ll create REST endpoints for Person.

Spring 5 added support for creating routes functionally while still supporting the traditional annotation-based way of creating them.

Let’s look at both of them with the help of examples.

6.1. Annotation-based

This is the traditional way of creating endpoints.

  
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonRespository personRespository;

    @GetMapping
    public Flux<Person> index() {
        return personRespository.findAll();
    }
}

This will create a REST endpoint /person which will return all the Person records reactively.

6.2. Router Functions

This is a new and concise way of creating endpoints.

  
@Bean
RouterFunction<?> routes(PersonRespository personRespository) {
    return nest(path("/person"),

            route(RequestPredicates.GET("/{id}"),
                request -> ok().body(personRespository.findById(request.pathVariable("id")), Person.class))

            .andRoute(method(HttpMethod.POST),
                request -> {
                    personRespository.insert(request.bodyToMono(Person.class)).subscribe();
                    return ok().build();
        })
    );
}

The nest method is used to create nested routes, where a group of routes share a common path (prefix), header, or other RequestPredicate.

So, in our case all the corresponding routes have the common prefix /person.

In the first route, we have exposed a GET API /person/{id} which will retrieve the corresponding record and return it.

In the second route, we have exposed a POST API /person which will receive a Person object and save it in the DB.

The cURL commands for the same:

  
curl http://localhost:8080/person -v -u tom:password
curl http://localhost:8080/person/{id} -v -u tom:password
curl http://localhost:8080/person -X POST -d '{"name":"John Doe","age":20}' -H "Content-Type: application/json" -v -u tom:password

We should define the routes in a Spring configuration file.

7. Security

We’ll be using a very simple basic authentication mechanism in our example.

  
@Bean
UserDetailsRepository userDetailsRepository() {
    UserDetails tom = withUsername("tom").password("password").roles("USER").build();
    UserDetails harry = withUsername("harry").password("password").roles("USER", "ADMIN").build();
    return new MapUserDetailsRepository(tom, harry);
}

We have added some users for our application and assigned different roles to them.

8. Conclusion

I have tried explaining, with a simple example, how to build a simple Reactive web application using Spring Boot.

You can read more about:

You can find the complete example for the Config Server & Library Service on Github.

Published on Java Code Geeks with permission by Mohit Sinha, partner at our JCG program. See the original article here: Introduction to Spring Cloud – Config (Part I)

Opinions expressed by Java Code Geeks contributors are their own.

Mohit sinha

Mohit Sinha is a Software Developer at Turtlemint, where he focuses mostly on the back-end. Prior to that he has worked on different types of projects which enhanced his skill-set in different ways. He enjoys writing about things he learns. He constantly searches for ways to create better software & applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button