之前一章我们介绍了如何搭建Eureka Server,这一章,我们介绍如何搭建服务提供者。
Eureka Clients介绍
服务的提供者,通过发送REST请求,将自己注册到注册中心(在高可用注册中心的情况下,提供者会分别注册到两台注册中心)。注册完成之后,会维护一个心跳来实现服务续约,告诉注册中心自己还存活,以防止被注册中心剔除。
搭建Eureka服务提供者
1、配置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> <groupId>com.SCClient</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SCClient</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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.RELEASE</spring-cloud.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
2、配置application.yml
spring:
application:
name: client
server:
port: 11112
eureka:
client:
service-url:
defaultZone: http://localhost:11110/eureka,http://localhost:11111/eureka
instance:
lease-renewal-interval-in-seconds: 10
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版本中可以不用该注释也可实现功能)。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class ScClientApplication { public static void main(String[] args) { SpringApplication.run(ScClientApplication.class, args); } }
4、创建Controller,提供服务。
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ClientController { @RequestMapping("/hello") public String hello() { return "hello this is client1"; } }
5、搭建多个相同服务的实例。该步骤不赘述,搭建方法和以上步骤一致,需要在application.yml中修改为不同端口号。
6、启动多个实例,访问http://localhost:{port}/{mapurl}来查看服务实例是否正确启动(如http://localhost:11112/hello)。并在两个注册中心中,可以看到服务实例被注册。至此,服务实例搭建成功。