Learning Spring-Cloud – Infrastructure and Configuration
I got a chance to play with Spring-Cloud to create a sample set of cloud ready microservices and I am very impressed by how Spring-Cloud enables different infrastructure components and services to work together nicely.
I am used to creating microservices based on Netflix OSS based stack and typically in a Netflix stack Eureka is considered the hub using which the microservices register themselves and discover each other. In the spirit of this model, I wanted to try out a series of services which look like this:
There are 2 microservices here:
- A sample-pong service which responds to “ping” messages
- A sample-ping service which uses the “pong” micro-service
And there are two infrastructure components:
- Sample-config which provides a centralized configuration for the 2 microservices
- Eureka which is the central hub providing a way for the services to register themselves and discover other services
So to start with, here I will introduce how I went about using spring-cloud to develop the two infrastructure components and follow it up with how the microservices can be developed to use these components.
- The entire project is available at my github location.
Eureka
Spring-cloud makes it very simple to bring up an instance of Eureka, all that is required is a class along the following lines:
package org.bk.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
Multiple instances of Eureka can be started up and can be configured to work together in a resilient way, here though I just want a demo standalone Eureka instance and this can be done using a configuration which looks like this, essentially starting up eureka on port 8761 and in a standalone mode by not trying to look for peers:
--- # application.yml server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false
Configuration Server
Spring-Cloud provides a centralized configuration server that microservices can use for loading up their properties. Typically microservices may want to go one of two ways:
- Use Eureka as a hub and find the configuration services
- Use Configuration services and find Eureka
I personally prefer the Eureka first approach, in this sample Configuration server registers itself with Eureka and when microservices come up they first check with Eureka, find the Configuration service and use the service to load up their properties.
The configuration server is simple to write using Spring-cloud too, the following is all the code that is required:
package org.bk.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
and the configuration that registers this service with Eureka:
--- # bootstrap.yml spring: application: name: sample-config profiles: active: native eureka: instance: nonSecurePort: ${server.port:8888} client: serviceUrl: defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
--- # application.yml spring: cloud: config: server: native: searchLocations: classpath:/config server: port: 8888
The configuration server is being started at port 8888, and provides configuration from the classpath. In a real application, the configuration can be set to load from a central git repository, this way providing a clean way to version properties and the ability to centrally manage the properties. In this specific case, since it provides properties for two microservices, there are two sets of files in the classpath and provide appropriate properties to the calling application:
--- #sample-pong.yml reply: message: Pong
--- # sample-ping.yml send: message: Ping
Starting up Eureka and Configuration Server
Since both these applications are Spring-boot based, they can each be started up by running the following command:
mvn spring-boot:run
Once Eureka and Configuration server come up cleanly., Eureka provides a nice interface with details of the services registered with it, in this case the Configuration server shows up with a name of “SAMPLE-CONFIG”:
The config server provides properties to the calling applications through endpoints with the pattern: /{application}/{profile}[/{label}]
So to retrieve the properties for “sample-pong” application, the following url is used internally by the application: http://localhost:8888/sample-pong/default
and for the “sample-ping” application the properties can be derived from http://localhost:8888/sample-ping/default
This concludes the details around bringing up the Infrastructure components of a Cloud ready system. I will follow it up with how the microservices can be developed that make use of these infrastructure components.
- The code behind these samples are available at mmy github repository.
Reference: | Learning Spring-Cloud – Infrastructure and Configuration from our JCG partner Biju Kunjummen at the all and sundry blog. |
Thanks for the article. A question:
I need to know and a right to ping pong request. I can not find out
Hi, This a copy/paste from spring official web site nothing more …
hi, im struggling to determine the correct project structure for running these mircoservices in eclipse.
eureka and config server need to be individual applications but yet share a parent pom according to your repo source? does this mean they follow the maven modules structure with each module defining its own pom? do they live in one java project or separate projects ?
How can I run this Spring Cloud with micro services in Codenvy,can you please post directions how to proceed?