1. 場景描述
前幾天介紹了下springcloud的Eureka注冊中心(springcloud-注冊中心快速構建),今天結合springboot-web介紹下eureka客戶端服務注冊。
2. 解決問題
2.1 新建eureka客戶端項目
2.1.1 new->project
2.1.2 項目名稱改一下
2.1.3 依賴包選擇
(1)選擇web的starter,等下跑測試使用。
(2)注冊中心客戶端依賴包選擇
next-》finish
2.2 項目介紹
三個類,就可以跑起來了。
2.2.1 pomx文件
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spc</groupId>
<artifactId>eurekaclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<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-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-actuator</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>
說明:就兩個starter,一個web的starter;一個eureka-client的。
2.2.2 啟動類
package com.spc.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class EurekaclientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "i'm 軟件老王,歡迎光臨,服務端口號為:" + port;
}
}
說明:
(1) 啟動類上有三個標簽,@SpringBootApplication、@RestController、@EnableEurekaClient。
(2)使用springmvc的標簽 @RequestMapping("/"),從request中拿到一個端口號打印下。
2.2.3 application.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
registry-fetch-interval-seconds: 5
server:
port: 9001
spring:
application:
name: client
重點說明:
defaultZone: http://localhost:8761/eureka/ ,這個地址是eureka注冊中心的地址(springcloud-注冊中k速構建),其他的幾個可有可無,其他幾個是為了能快速看到注冊信息設置的心跳時間,可忽略。
2.3 效果圖
本地通過不同端口號啟動eureka注冊中心和客戶端服務注冊(springcloud啟動多個實例)