Spring Cloud and Spring Boot. Part 1: using Eureka Server

We invite future students of the Spring Framework Developer course and everyone to an open online lesson on the topic “Introduction to the clouds, creating a cluster in Mongo DB Atlas018” . Participants, together with an expert teacher, will talk about cloud types and set up a free Mongo DB cluster for their projects.



And now we are sharing with you the traditional translation of the article.






In this article, we will talk about how to install and configure service discovery for Java microservices.





What is Eureka Server?

Eureka Server is service discovery for your microservices. Client applications can self-register with it, and other microservices can contact Eureka Server to find the microservices they need.





Eureka Server Discovery Server IP- .





Eureka Server pom.xml



.





<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
      
      



Eureka Server

https://start.spring.io . , Group, Artifact, / . "Generate Project" zip-. IDE Maven-.





  • Eureka Server





  • Web





  • Actuator





, pom.xml



:





<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.7.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example.eureka.server</groupId>
   <artifactId>eureka-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>eureka-server</name>
   <description>Demo project for Spring Boot</description>

   <properties>
       <java.version>1.8</java.version>
       <spring-cloud.version>Finchley.SR2</spring-cloud.version>
   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${spring-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>
</project>
      
      



EurekaServerApplication.java



@EnableEurekaServer



, .





package com.example.eureka.server.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

  public static void main(String[] args) {
     SpringApplication.run(EurekaServerApplication.class, args);
  }
}
      
      



application.properties



, src/main/resources



, .





spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
      
      



  • spring.application.name



    — .





  • server.port



    — , , (8761).





  • eureka.client.register-with-eureka



    — , Eureka Server.





  • eureka.client.fetch-registry



    — .





Eureka Java- http://localhost:8761/ 





, Eureka Server , .





Eureka Server

https://start.spring.io . , Group, Artifact, / . "Generate Project" zip-. IDE Maven-.





  • DevTools





  • Actuator





  • Discovery Client





, pom.xml



:





<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.0.7.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example.eureka.client</groupId>
  <artifactId>EurekaClientApplication</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>EurekaClientApplication</name>
  <description>Demo project for Spring Boot</description>
 
  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <java.version>1.8</java.version>
     <spring-cloud.version>Finchley.SR2</spring-cloud.version>
  </properties>
 
  <dependencies>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
     </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
  </dependencies>
 
  <dependencyManagement>
     <dependencies>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>${spring-cloud.version}</version>
           <type>pom</type>
           <scope>import</scope>
        </dependency>
     </dependencies>
  </dependencyManagement>
  <build>
     <plugins>
        <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
     </plugins>
  </build>
</project>
      
      



EurekaClientApplication.java



@EnableDiscoveryClient



, .





package com.example.eureka.client.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

  public static void main(String[] args) {
     SpringApplication.run(EurekaClientApplication.class, args);
  }
}
      
      



REST- 

HelloWorldController



com.example.eureka.client.application



GET- .





package com.example.eureka.client.application;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

   @GetMapping("/hello-worlds/{name}")
   public String getHelloWorld(@PathVariable String name) {
       return "Hello World " + name;
   }
}
      
      



application.properties



, src/main/resources, .





spring.application.name=eureka-client-service
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
      
      



eureka.client.service-url.defaultZone



Eureka Server, .





, Eureka Server . Java- Eureka Server http://localhost:8761/. , Eureka Server.





Eureka Server . (Distributed Tracing) Spring Boot-.






« Spring Framework».



-
« , Mongo DB Atlas018».












All Articles