之前一章我們介紹了如何搭建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)。並在兩個注冊中心中,可以看到服務實例被注冊。至此,服務實例搭建成功。