1. 程式人生 > >spring cloud 實現Eureka註冊服務

spring cloud 實現Eureka註冊服務

服務註冊和發現

原文網址:https://spring.io/guides/gs/service-registration-and-discovery/

What you'll build

你將配置一個Eureka service註冊中心和一個client,client能自動註冊到註冊中心來解決埠問題。一個服務註冊中心非常有用,它使得client-side負載均衡,並從 consumers分離服務的providers而不需要DNS。

What you'll need

15分鐘

一個自己喜歡的編輯器或IDE

JDK1.8以上

Gradle 2.3+ 或者 Maven 3.0+

How to complete this guide

獲取原始碼

建立gradle工程,設定build.gradle

eureka-service/build.gradle

buildscript {
	ext {
		springBootVersion ='1.5.2.RELEASE'}
	repositories {
		mavenCentral()}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}}

apply plugin:'java'
apply plugin:'eclipse'
apply plugin:'idea' apply plugin:'org.springframework.boot' jar { baseName ='eureka-service' version ='0.0.1-SNAPSHOT'} sourceCompatibility =1.8 targetCompatibility =1.8 repositories { mavenCentral()} dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
}} dependencies { compile('org.springframework.cloud:spring-cloud-starter-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test')} eclipse { classpath { containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'}}
eureka-client/build.gradle
buildscript {
	ext {
		springBootVersion ='1.5.2.RELEASE'}
	repositories {
		mavenCentral()}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}}

apply plugin:'java'
apply plugin:'eclipse'
apply plugin:'idea'
apply plugin:'org.springframework.boot'

jar {
	baseName ='eureka-client'
	version ='0.0.1-SNAPSHOT'}
sourceCompatibility =1.8
targetCompatibility =1.8

repositories {
	mavenCentral()}

dependencyManagement {
	imports {
		mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'}}

dependencies {
	compile('org.springframework.cloud:spring-cloud-starter-eureka')
	compile('org.springframework.boot:spring-boot-starter-web')
	testCompile('org.springframework.boot:spring-boot-starter-test')
	testCompile('org.springframework.cloud:spring-cloud-starter-eureka-server')}

eclipse {
	classpath {
		 containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
		 containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'}}
Spring Boot Gradle Plugin提供了許多便利的特性:
  • 它收集classpath中所有的jar包,並build一個獨立的,可執行的“uber-jar”,使得執行和傳遞服務更為方便
  • 它尋找public static void main()方法來標記可執行的類
  • 它提供一個嵌入的依賴解析器,設定版本號匹配Spring Boot依賴。你可以重寫成任意版本,但是他將預設為Boot的選擇版本集。

build with Maven

建立Maven工程

eureka-server/pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.example</groupId><artifactId>eureka-service</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/><!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR5</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>

eureka-client/pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.example</groupId><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/><!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR5</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>

Spring Boot Maven plugin提高了許多方便的特性: 同上 gradle plugin

build with your IDE
Stand up a Eureka Service Registry

你首先需要一個Eureka Service註冊中心,你可以使用Spring Cloud的@EnableEurekaServer來建立註冊中心,以便其他應用訪問。這是一個普通的Spring Boot應用,加上註解來提供服務註冊功能。

service中建立一個application.java

package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublicclassEurekaServiceApplication{publicstaticvoid main(String[] args){SpringApplication.run(EurekaServiceApplication.class, args);}}

當註冊中心啟動時,會發生錯誤,帶有堆疊資訊:“沒有註冊中心可連線的副本節點”。在生產環境下,你想要註冊中心不止一個例項,然而,出於簡單的目的,它足以使相關日誌失效。

預設情況下,註冊中心也將試圖將自己也註冊上,所以你也需要禁止它註冊。

一般在本地使用時,協定將註冊中心放在一個單獨的埠上。

新增一些屬性來處理這些要求:

配置檔案eureka-service/src/main/resources/application.properties
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

Talking to the Registry

現在我們已經搭建好註冊中心了,現在搭建一個客戶端,讓它在註冊中心自己註冊,並且使用Spring Cloud DiscoveryClient抽象去註冊中心詢問主機和埠。@EnableDiscoveryClient啟用Netflix Eureka DiscoveryClient實現。還有其他實現用於其他服務註冊中心,如Hashicorp’s Consul or Apache Zookeeper.

client中建立application.java

package hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@EnableDiscoveryClient@SpringBootApplicationpublicclassEurekaClientApplication{publicstaticvoid main(String[] args){SpringApplication.run(EurekaClientApplication.class, args);}}@RestControllerclassServiceInstanceRestController{@AutowiredprivateDiscoveryClient discoveryClient;@RequestMapping("/service-instances/{applicationName}")publicList<ServiceInstance> serviceInstancesByApplicationName(@PathVariableString applicationName){returnthis.discoveryClient.getInstances(applicationName);}}
無論你選擇哪種實現,你將發現eureka-client註冊在了你在spring.application.name定義的屬性名下。這個屬性在Spring Cloud中用的很多,通常在服務配置的最初階段,此屬性用在service bootstrap,所以為了方便,將它放在: eureka-client/src/main/resources/bootstrap.properties此檔案將先於src/main/resources/application.properties被發現

eureka-client/src/main/resources/bootstrap.properties

spring.application.name=a-bootiful-client

eureka-client定義一個Spring MVC REST端點,ServiceInstanceRestController,它將返回一個列舉,其中包含了註冊中心在http://localhost:8080/service-instances/a-bootiful-client註冊的所有ServiceInstance例項。查閱Building a RESTful Web Service指導來學習更多關於用Spring MVC 和 Spring Boot建立REST服務。

Test the application

測試end-to-end(端對端)結果,首先啟動eureka-service,載入完畢後,啟動eureka-client,客戶端將花一分鐘將它註冊到註冊中心,並從註冊中心重新整理它自己已註冊是例項列表。所有的這些閾值都是可配置的,在瀏覽器中訪問客戶端http://localhost:8080/service-instances/a-bootiful-client。你將看到客戶端的ServiceInstance對映在響應中。

遇到的問題:

  1. 客戶端註冊不成功,找不到server,解決辦法:在bootstrap.properties中配置server地址:eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

相關推薦

spring cloud 實現Eureka註冊服務

服務註冊和發現 原文網址:https://spring.io/guides/gs/service-registration-and-discovery/ What you'll build 你將配置一個Eureka service註冊中心和一個client,client能自

基於spring-cloud實現eureka註冊服務小案例

首先建立兩個專案,eureka-service和eureka-client。 eureka-server作為eureka的服務端,提供註冊服務,eureka-client作為eureka的客戶端,屬於一個應用,註冊到eureka註冊中心。 eureka-

spring-cloudeureka註冊中心及服務釋出

最近在看spring cloud相關技術,剛看到eureka部分,記錄下。 對於spring-cloud,讓開發者只關注自己的業務邏輯,不再因為配置檔案的繁瑣而憂愁。 實現註冊中心eureka-server比較簡單,重點在於配置檔案application.yml eurek

Spring Cloud 入門Eureka -Consumer服務消費(一)

ppi package AR con .so 1.8 Coding ng- int 這裏介紹:LoadBalancerClient接口,它是一個負載均衡客戶端的抽象定義,下面我們就看看如何使用Spring Cloud提供的負載均衡器客戶端接口來實現服務的消費。 引用之前的文

Spring Cloud 入門Eureka -Consumer服務消費(聲明式Feign)(三)

load control interface github example TP oca des 整合   Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。我們只需要通過創建接口並用

Spring Cloud入門程序——註冊服務提供者

ann disco align prope not -a out net col 1、創建Spring Starter project 2、引入依賴 點擊finish 3、創建啟動類 package com.hello; import org.spri

Spring CloudEureka註冊中心叢集搭建

Spring Cloud也包含了許多的子專案 , 下面等下要使用的Eureka只是其中的一個子專案  Eureka的功

Spring-Cloud-Netflix-Eureka註冊中心

目錄 概述 Netflix-Eureka與SpringCloud的關係 Eureka原理 CAP定理(瞭解) (Spring-Cloud-Netflix-Eureka註冊中心)

spring-cloud(一)服務註冊與發現Eureka(Finchley版本)

spring cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件匯流排、全域性鎖、決策競選、分散式會話等等。它執行環境簡單,可以在開發人員的電腦上跑。另外說明spring cloud是基於springboot的,所以需要開發中對springb

Spring CloudEureka服務註冊中心的搭建

1:首先在idea上面建立一個Maven工程,命名為Sprng-Boot-eureka-test 建立的Maven工程中的pom檔案如下 2:在建立的Sprng-Boot-eureka-test工程下面新建一個module為eureka-server

Spring Cloud-03將微服務註冊Eureka Server上 + 為Eureka Server新增使用者認證

文章目錄 概述 將使用者微服務micorservice-provider-user註冊到Eureka Server上 pom中增加 spring-cloud-starter-netflix-eureka-client 依賴 啟動類新增@

十五:Spring CloudEureka服務註冊中心(HA版)

1. Eureka簡介 2. 程式碼實現 2.1涉及的模組 eureka-server-ha:通過profiles指定不同的埠來模擬多服務例項。 eureka-service:服務提供者 2.2

spring cloud 入門(二)【 客戶端往註冊中心Eureka 註冊服務

客戶端 往Eureka 註冊服務,註冊成功之後,其他的服務,才可以對本服務進行呼叫 程式碼結構如下:   UserApplication 程式碼如下: package com.study.user; import org.springframework.boot.

初始化Spring Cloud建立Eureka服務註冊中心

bootstrap https 控制臺 depend lee 心跳 git reg iyu 1、新建項目 https://gitee.com/frankawp/vishnu 新建一個git項目 打開git bash git clone https://gitee.co

Spring Boot + Spring Cloud 實現許可權管理系統 後端篇(十八):服務註冊和發現(Consul)

什麼是 Consul Consul 是 HashiCorp 公司推出的開源工具,用於實現分散式系統的服務發現與配置。與其它分散式服務註冊與發現的方案,Consul 的方案更“一站式”,內建了服務註冊與發現框架、分佈一致性協議實現、健康檢查、Key/Value 儲存、多資料中心方案,不再需要依賴其它工具(比如

Spring Cloud學習一:服務治理Spring Cloud Eureka搭建高可用註冊中心

Spring Cloud Eureka是Spring Cloud NetFlix微服務套件中的一部分,基於NetFlix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能,是微服務架構中最為核心和基礎的模組,既包含了服務端元件,也包含了客戶端元件。 又涉及到服務註冊與服務發現兩個

Spring Cloud Eureka ——註冊服務提供者

1pom.xml引入依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-e

Spring Cloud(一)服務註冊與發現(Eureka

Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它為基於JVM的雲應用開發中涉及的配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、全域性鎖、決策競選、分散式會話和叢集狀態管理等操作提供了一種簡單的開發方式。 Spring Clou

Spring CloudEureka服務註冊與發現

Eureka 雲端服務發現,一個基於 REST 的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。引用[Spring Cloud中文網] 在Spring Cloud中使用Eureka來進行服務註冊與發現,通過Eureka管理各個微服務(感覺類似Zookeeper),

03.Spring Cloud學習筆記之服務註冊服務發現元件Eureka

前言 從本篇部落格開始將正式進入Spring Cloud的實戰部分,因為博主用了很長時間的Dubbo,並且Spring Cloud和Dubbo都是微服務框架,它們有很多相似之處,所以可能在部落格中提及進行類比,如果沒有接觸過Dubbo的朋友直接略過該部分內容即