Eureka is a service discovery framework developed by Netflix itself is a REST-based services.
Eureka consists of two components: Eureka Server and Eureka Client.
Content from: https://www.jianshu.com/p/c18d140ad9f6
IDEA can be used to create a new project in the spring initializr such micro-services.
Add dependence.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
In the startup class, annotated @EnableEurekaServer, using eureka.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
Add configuration, and port names are basic configuration, the micro-services in multiple service consisting of, it is a must.
spring:
application:
name: eureka
server:
port: 8761
eureka service configuration server side. In the actual project, in order to engage in is available, use multiple eureka registry.
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
-
service-url是一个map。而且指定了 defaultZone 。按住ctrl,鼠标点击 service-url ,在源码里可以找到 规则。
private Map<String, String> serviceUrl = new HashMap(); this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/"); public void setServiceUrl(Map<String, String> serviceUrl) { this.serviceUrl = serviceUrl; }
- register-with-eureka 默认是 true,表示会被 eureka发现。false,表示不在 http://localhost:8761/eureka/ 这个注册中心上发现自己。
启动
完整的pom如下:
<?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 https://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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jin</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<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>