Spring Cloud und Spring Boot. Teil 1: Verwenden von Eureka Server

Wir laden zukünftige Studenten des Spring Framework Developer- Kurses und alle zu einer offenen Online-Lektion zum Thema „Einführung in die Clouds, Erstellen eines Clusters in Mongo DB Atlas018“ ein . Die Teilnehmer werden zusammen mit einem erfahrenen Lehrer über die Arten von Clouds sprechen und einen kostenlosen Mongo DB-Cluster für ihre Projekte einrichten.



Und jetzt teilen wir Ihnen die traditionelle Übersetzung des Artikels mit.






In diesem Artikel wird erläutert, wie die Serviceerkennung für Java-Microservices installiert und konfiguriert wird.





Was ist Eureka Server?

Eureka Server ist eine Serviceerkennung für Ihre Microservices. Clientanwendungen können sich selbst registrieren, und andere Microservices können auf Eureka Server zugreifen, um die benötigten Microservices zu finden.





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