Microservices with Spring Boot 2.0, Eureka and Spring Cloud

There are many articles on my blog about microservices with Spring Boot and Spring Cloud. The main purpose of this article is to provide a brief summary of the most important components provided by these frameworks that help you in creating microservices. The topics covered in this article are:
    • Using Spring Boot 2.0 in cloud-native development
    • Providing service discovery for all microservices with Spring Cloud Netflix Eureka
    • Distributed configuration with Spring Cloud Config
    • API Gateway pattern using a new project inside Spring Cloud: Spring Cloud Gateway
    • Correlating logs with Spring Cloud Sleuth
Before we proceed to the source code, let’s take a look on the following diagram. It illustrates the architecture of our sample system. We have three independent microservices, which register themself in service discovery, fetch properties from configuration service and communicate with each other. The whole system is hidden behind API gateway.
Currently, the newest version of Spring Cloud is Finchley.SR1. This version of spring-cloud-dependencies should be declared as a BOM for dependency management.


















<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>

</dependencyManagement>

Now, let’s consider the further steps to be taken in order to create working microservices-based system using Spring Cloud. We will begin from Configuration Server.

Step 1. Building configuration server with Spring Cloud Config


To enable Spring Cloud Config feature for an application, first include spring-cloud-config-server to your project dependencies.
1
2
3
4
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
Then enable running embedded configuration server during application boot use @EnableConfigServer annotation.
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConfigApplication.class).run(args);
    }
}
By default Spring Cloud Config Server store the configuration data inside Git repository. This is very good choice in production mode, but for the sample purpose file system backend will be enough. It is really easy to start with config server, because we can place all the properties in the classpath. Spring Cloud Config by default search for property sources inside the following locations: classpath:/, classpath:/config, file:./, file:./config.
We place all the property sources inside src/main/resources/config. The YAML filename will be the same as the name of service. For example, YAML file for discovery-service will be located here: src/main/resources/config/discovery-service.yml.
And last two important things. If you would like to start config server with file system backend you have activate Spring Boot profile native. It may be achieved by setting parameter --spring.profiles.active=native during application boot. I have also changed the default config server port (8888) to 8061 by setting property server.port in bootstrap.yml file.

Step 2. Building service discovery with Spring Cloud Netflix Eureka

More to the point of configuration server. Now, all other applications, including discovery-service, need to add spring-cloud-starter-configdependency in order to enable config client. We also have to include dependency to spring-cloud-starter-netflix-eureka-server.
1
2
3
4
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Then you should enable running embedded discovery server during application boot by setting @EnableEurekaServer annotation on the main class.
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(DiscoveryApplication.class).run(args);
    }
}

Application has to fetch property source from configuration server. The minimal configuration required on the client side is an application name and config server’s connection settings.
1
2
3
4
5
6
spring:
  application:
    name: discovery-service
  cloud:
    config:
      uri: http://localhost:8088
As I have already mentioned, the configuration file discovery-service.ymlshould be placed inside config-service module. However, it is required to say a few words about the configuration visible below. We have changed Eureka running port from default value (8761) to 8061. For standalone Eureka instance we have to disable registration and fetching registry.
1
2
3
4
5
6
7
8
9
10
11
server:
  port: 8061
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Now, when you are starting your application with embedded Eureka server you should see the following logs.



@Reference: https://piotrminkowski.wordpress.com/2018/04/26/quick-guide-to-microservices-with-spring-boot-2-0-eureka-and-spring-cloud/

Git Url: https://github.com/JavaHelper/spring-cloud-howtodoinjava/tree/master/sample-spring-microservices-new_2.0.4_RELEASE




C:\>curl http://localhost:8091/1
{"id":1,"organizationId":1,"name":"Development","employees":[{"id":1,"name":"John Smith","age":34,"position":"Analyst"},{"id":2,"name":"Darren Hamilton","age":37,"position":"Manager"},{"id":3,"name":"Tom Scott","age":26,"position":"Developer"}]}

C:\>curl http://localhost:8091/organization/1

[{"id":1,"organizationId":1,"name":"Development","employees":[{"id":1,"name":"John Smith","age":34,"position":"Analyst"},{"id":2,"name":"Darren Hamilton","age":37,"position":"Manager"},{"id":3,"name":"Tom Scott","age":26,"position":"Developer"}]},{"id":2,"organizationId":1,"name":"Operations","employees":[{"id":4,"name":"Anna London","age":39,"position":"Analyst"},{"id":5,"name":"Patrick Dempsey","age":27,"position":"Developer"}]}]

C:\>curl http://localhost:8060/organization/1/with-departments-and-employees

{"id":1,"name":"Microsoft","address":"Redmond, Washington, USA","departments":[{"id":1,"name":"Development","employees":[{"id":1,"name":"John Smith","age":34,"position":"Analyst"},{"id":2,"name":"Darren Hamilton","age":37,"position":"Manager"},{"id":3,"name":"Tom Scott","age":26,"position":"Developer"}]},{"id":2,"name":"Operations","employees":[{"id":4,"name":"Anna London","age":39,"position":"Analyst"},{"id":5,"name":"Patrick Dempsey","age":27,"position":"Developer"}]}],"employees":[]}

Comments

Popular posts from this blog

Install Redis on Oracle Linux 7+