Spring Cloud y Spring Boot. Parte 1: uso de Eureka Server

Invitamos a los futuros estudiantes del curso Spring Framework Developer ya todos a una lección en línea abierta sobre el tema “Introducción a las nubes, creación de un clúster en Mongo DB Atlas018” . Los participantes, junto con un profesor experto, hablarán sobre los tipos de nubes y establecerán un clúster Mongo DB gratuito para sus proyectos.



Y ahora compartimos contigo la traducción tradicional del artículo.






En este artículo, hablaremos sobre cómo instalar y configurar el descubrimiento de servicios para microservicios Java.





¿Qué es Eureka Server?

Eureka Server es el descubrimiento de servicios para sus microservicios. Las aplicaciones cliente pueden auto-registrarse con él, y otros microservicios pueden acceder a Eureka Server para encontrar los microservicios que necesitan.





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