Let's use Spring Initializr (https://start.spring.io) to get started with Microservice A. Choose GroupId, ArtifactId, and the frameworks, as shown in the following screenshot:
We will create a service to expose a set of random numbers:
@RestController
public class RandomNumberController {
private Log log =
LogFactory.getLog(RandomNumberController.class);
@RequestMapping("/random")
public List<Integer> random() {
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
numbers.add(generateRandomNumber());
}
log.warn("Returning " + numbers);
return numbers;
}
private int generateRandomNumber() {
return (int) (Math.random() * 1000);
}
}
Important things to note are as follows:
- @RequestMapping("/random") public List<Integer> random(): Random service returns a list of random numbers
- private int generateRandomNumber() {: Generates random numbers between 0 and 1000
The following snippet shows a sample response from the service at http://localhost:8080/random:
[666,257,306,204,992]
Next, we would want to create a service to return a simple message from the application configuration in application.properties.
Let's define a simple application configuration with one property--message:
@Component
@ConfigurationProperties("application")
public class ApplicationConfiguration {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
A few important things to note are as follows:
- @ConfigurationProperties("application"): Defines a class defining application.properties.
- private String message: Defines one property--message. The value can be configured in application.properties with application.message as the key.
Let's configure application.properties, as shown in the following snippet:
spring.application.name=microservice-a
application.message=Default Message
A couple of important things to note are as follows:
- spring.application.name=microservice-a: spring.application.name is used to give a name to the application
- application.message=Default Message: Configures a default message for application.message
Let's create a controller to read the message and return it, as shown in the following snippet:
@RestController
public class MessageController {
@Autowired
private ApplicationConfiguration configuration;
@RequestMapping("/message")
public Map<String, String> welcome() {
Map<String, String> map = new HashMap<String, String>();
map.put("message", configuration.getMessage());
return map;
}
}
Important things to note are as follows:
- @Autowired private ApplicationConfiguration configuration: Autowires ApplicationConfiguration to enable reading the configured message value.
- @RequestMapping("/message") public Map<String, String> welcome(): Exposes a simple service at the URI/message.
- map.put("message", configuration.getMessage()): The service returns a map with one entry. It has a key message and the value is picked up from the ApplicationConfiguration.
When the service is executed at http://localhost:8080/message, we get the following response:
{"message":"Default Message"}