Eureka Clients介绍
服务的提供者,通过发送REST请求,将自己注册到注册中心(在高可用注册中心的情况下,提供者会分别注册到两台注册中心)。注册完成之后,会维护一个心跳来实现服务续约,告诉注册中心自己还存活,以防止被注册中心剔除。
工程结构图:
1. pom.xml文件
添加eureka-client 01,eureka-client 02,eureka-client 03三部分内容
<?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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lee</groupId>
<artifactId>EurekaClients01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EurekaClients01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- eureka-client 02-->
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client 01-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- eureka-client 03-->
<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>
2.application.properties 文件
spring.application.name=client01
server.port=11112
eureka.client.service-url.defaultZone=http://localhost:10001/eureka,http://localhost:11111/eureka
eureka.client.instance.lease-renewal-interval-in-seconds=10
eureka.client.register-with-eureka=true
配置项说明
- spring.application.name:用于配置应用名,该名称会作为注册中心中的服务名,调用者也通过调用该服务名实现服务的调用,不同服务不同服务名称,相同服务配置为同一个,不区分大小写。
- eureka.client.serverUrl.defaultZone:配置注册中心地址,多个以逗号隔开。
- eureka.client.instance.lease-renewal-interval-in-seconds:配置心跳续约周期时间间隔,默认为30秒。注册中心以该心跳来识别实例状态。
- eureka.client.register-with-eureka:默认为true,表示将自己注册到注册中心。
3. 配置启动项,使用@EnableEurekaClient开启服务发现(2.0版本中可以不用该注释也可实现功能)。
EurekaClients01Application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClients01Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClients01Application.class, args);
}
}
4. 创建Controller,提供服务,ClientController.java
package com.example.demo.controler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
@RequestMapping("client")
public class ClientController {
@ResponseBody
@RequestMapping("hello")
public String hello() {
return "hello world...";
}
}
5、搭建多个相同服务的实例。该步骤不赘述,搭建方法和以上步骤一致,需要在application.properties中修改为不同端口号。
6、启动多个实例,访问http://localhost:{port}/{mapurl}来查看服务实例是否正确启动(如http://localhost:11112/hello)。并在两个注册中心中,可以看到服务实例被注册。至此,服务实例搭建成功。
启动时需要先启动注册中心EurekaServer项目,再启动此项目。