How to do it...

Let us create our first Eureka cloud-based services through these steps:

  1. First, create a Spring Boot application that will be deployed and run as a Eureka server and nothing else. Name the project ch10-eureka-hrs. It should contain only the core starter POM dependencies such as the spring-boot-starter-webflux since this will just access the microservices.
  2. To enable Spring Cloud Finchley modules, add the following dependency management component inside the pom.xml:
<dependencyManagement> 
    <dependencies> 
        <dependency> 
            <groupId>org.springframework.cloud</groupId> 
            <artifactId>spring-cloud-dependencies</artifactId> 
            <version>Finchley.M1</version> 
            <type>pom</type> 
            <scope>import</scope>   
        </dependency> 
    </dependencies> 
</dependencyManagement>
  1. Afterwards, add the following Spring Cloud starter POM to the Maven dependencies of our pom.xml:
<dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-starter</artifactId> 
</dependency> 
<dependency> 
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter-eureka</artifactId> 
</dependency> 
<dependency> 
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter-eureka-server</artifactId> 
</dependency> 
  1. Also add the actuator module and enable the /shutdown endpoint to shut down the application:
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-actuator</artifactId> 
</dependency> 
  1. Now, create the core package org.packt.microservice.server that contains the bootstrap class HRSEurekaBootApplication that will purposely load and execute all the needed Eureka management and monitoring commands once deployed. The indicator that it is a Eureka server is the class-level annotation @EnableEurekaServer found in the bootstrap:
@SpringBootApplication 
@EnableEurekaServer 
public class HRSEurekaBootApplication { 
   
  public static void main(String[] args) { 
     SpringApplication.run(HRSEurekaBootApplication.class, args); 
  } 
}
  1. In src/main/resources, add an application.properties that will auto-configure this application to become a server only because, by default, Eureka could be both the client and the server which can be a mess when it comes to huge microservice design architecture. Likewise, add all the needed properties for the actuator endpoint:
eureka.instance.hostname=localhost 
spring.application.name=eureka 
server.port=5566 
eureka.client.register-with-eureka=false 
eureka.client.fetch-registry=false 
 
spring.datasource.jmx-enabled=false 
 
management.port=5566 
management.address=localhost 
management.context-path=/appdetails 
 
endpoints.info.enabled=true 
endpoints.info.sensitive=false 
endpoints.info.id=info 
info.app.description=HRS Eureka Server 
info.app.version=1.0.0 
 
endpoints.sensitive=false 
endpoints.shutdown.sensitive=false 
endpoints.shutdown.enabled=true 
  1. Save all files. Run clean spring-boot:run -U to deploy the application. Open a browser and access http://localhost:5566/:
  1. Although you can convert the previous ch10-deptservice, ch10-empservice, and ch10-loginservice to become the Eureka clients, this recipe decided to create three separate instances which expose the same RESTful services. To start, create an empty Maven project ch10-depts-instance whose pom.xml is the same as ch10-deptservice with the inclusion of Spring Cloud Finchley dependency management component.
  1. Given the added plug-in, add the following Spring Cloud starter POM dependencies needed to set up a Eureka client instance:
<dependency> 
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter</artifactId> 
</dependency> 
<dependency> 
   <groupId>org.springframework.cloud</groupId> 
   <artifactId>spring-cloud-starter-eureka</artifactId> 
</dependency> 
  1. Then, create the core package org.packt.microservice.instance with the bootstrap class DeptInstanceBootApplication. To enable Eureka server registration of this instance, add the @EnableDiscoveryClient or @EnableEurekaClient to the bootstrap. These annotations will tell the Eureka server to explicitly discover this instance with its port and register it to its instance store:
@SpringBootApplication 
@EnableDiscoveryClient 
public class DeptInstanceBootApplication { 
     
    public static void main(String[] args) throws Exception { 
         SpringApplication.run(DeptInstanceBootApplication.class, 
        args); 
    } 
}
  1. In src/main/resources, add an application.properties which will contain the same properties in ch10-deptservice combined with some Eureka client details for this Department microservice instance. Once an exception happens during endpoint execution, Eureka will redirect the execution to eureka.client.service-url.defaultZone. Moreover, the server must be allowed to determine the status of the instance, as per /health of the actuator module. The Eureka client will only report its health status to Eureka server by enabling eureka.client.service-url.healthcheck.enabled property:
spring.application.name=depts-service-instance 
eureka.client.service-url.defaultZone= 
http://localhost:5566/eureka/ 
eureka.client.service-url.healthcheck.enabled=true 
# the rest of the properties from ch10-deptservice
  1. Copy all the configuration classes, controllers, handler and route functions, services, JPA repository classes, and entity models from ch10-deptservice and drop them to their new packages. Also, be sure to update the JPA repository base packages configured in SpringDataConfig.
  2. Save all files. Run clean spring-boot:run -U Maven commands to deploy the Eureka client. Open http://localhost:5566 again and check that Department Eureka instance is declared as part of the service registry.
  1. Perform again Steps 8 to 13 to build the Employee and Login microservice instances. After a long and successful procedure, run the Eureka server again and check if all three instances are registered.
  1. Run the following endpoints directly from the Eureka server to validate that all the instances are ready for execution. All the services will run using the IP address of the machine detected by the Eureka server:
  • http://alibata:8960/selectReactDepts (from the Department instance)
  • http://alibata:8965/selectReactEmps (from the Employee instance)
  • http://alibata:8970/fluxJpaUsers (from the Login instance)
  1. Observe the status of your Eureka server. Once Eureka's capacity to renew instances per minute becomes lower than the expected renews from its instances, the message is thrown by the server similar to what is shown below. This occurrence happens when some connection issues are experienced by the Eureka server while some of the instances are expiring due to some heartbeat status. To avoid further microservice inter-communication problems, we just leave these instances preserved even if some are due for expiration (for example, self-preservation mode).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59