前言:
Eureka是基於REST(Representational State Transfer)服務,主要以AWS雲服務爲支撐,提供服務發現並實現負載均衡和故障轉移。我們稱此服務爲Eureka服務。Eureka提供了Java客戶端組件,Eureka Client,方便與服務端的交互。客戶端內置了基於round-robin實現的簡單負載均衡。在Netflix,爲Eureka提供更爲複雜的負載均衡方案進行封裝,以實現高可用,它包括基於流量、資源利用率以及請求返回狀態的加權負載均衡。
本文將基於Springboo-2.0以上版本和SpringClound-Finchley版來搭建註冊中心。
1、配置Eureka服務器
1、1引入Maven依賴
本項目是基於Springboot的,首先引入springboot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
引入SpringCloud
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在引入springcloud之後要配置以下內容
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
最後引入eureka服務端jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在項目下配置application.yml文件
server:
port: 9200
#spring:
# application:
# name: app_eurake_server01
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
fetch-registry: false
#自己註冊到eureka服務端
register-with-eureka: false
因爲eureka既可以作爲服務器端也可以作爲客戶端,所以在配置Eureka集羣的時候,將上面兩個false改成true。
配置啓動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurakeApplication {
public static void main(String[] args) {
SpringApplication.run(EurakeApplication.class, args);
}
}
使用註解@EnableEurekaServer註解來標識服務端。
啓動之後,訪問eureka服務器地址
已經可以正常訪問,通過eureka的後臺管理頁面,我們可以看到,服務被成功的註冊到註冊中心。
後面還會記錄eureka的集羣配置,和相關配置參數詳解。