Spring Cloud et Spring Boot. Partie 1: utilisation du serveur Eureka

Nous invitons les futurs étudiants du cours Spring Framework Developer et tout le monde à une leçon en ligne ouverte sur le thème «Introduction aux clouds, création d'un cluster dans Mongo DB Atlas018» . Les participants, avec un enseignant expert, parleront des types de nuages ​​et mettront en place un cluster Mongo DB gratuit pour leurs projets.



Et maintenant, nous partageons avec vous la traduction traditionnelle de l'article.






Dans cet article, nous expliquerons comment installer et configurer la découverte de services pour les microservices Java.





Qu'est-ce qu'Eureka Server?

Eureka Server est la découverte de services pour vos microservices. Les applications clientes peuvent s'enregistrer avec lui et d'autres microservices peuvent accéder à Eureka Server pour trouver les microservices dont ils ont besoin.





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