Spring Cloud 學(xué)習筆記 - No.1 服務(wù)注冊發(fā)現(xiàn) Eureka

Spring Cloud 介紹

官方網(wǎng)址 http://projects.spring.io/spring-cloud/

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state).
Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

Spring Cloud是一個基于Spring Boot實現(xiàn)的云應(yīng)用開發(fā)工具彩库,它為開發(fā)分布式應(yīng)用提供了一些列工具阁猜,例如配置管理麦牺、服務(wù)發(fā)現(xiàn)责语、斷路器棺牧、智能路由、微代理鸟赫、控制總線负芋、one-time tokens、全局鎖调窍、決策競選宝冕、分布式會話和集群狀態(tài)管理等。

Spring Cloud專注于提供良好的開箱即用的特性邓萨,包括:

  • 分布式/版本化配置 Distributed/versioned configuration
  • 服務(wù)注冊和發(fā)現(xiàn) Service registration and discovery
  • 路由 Routing
  • Service-to-service 調(diào)用 Service-to-service calls
  • 負載均衡 Load balancing
  • 斷路器 Circuit Breakers
  • 全局鎖 Global locks
  • 決策競選和集群狀態(tài)管理 Leadership election and cluster state
  • 分布式消息傳遞 Distributed messaging

Spring Cloud 的子項目

  • Spring Cloud Config
    Centralized external configuration management backed by a git repository. The configuration resources map directly to Spring Environment but could be used by non-Spring applications if desired.

  • Spring Cloud Netflix
    Integration with various Netflix OSS components (Eureka, Hystrix, Zuul, Archaius, etc.).

  • Spring Cloud Bus
    An event bus for linking services and service instances together with distributed messaging. Useful for propagating state changes across a cluster (e.g. config change events).

  • Spring Cloud for Cloud Foundry
    Integrates your application with Pivotal Cloud Foundry. Provides a service discovery implementation and also makes it easy to implement SSO and OAuth2 protected resources.

  • Spring Cloud Open Service Broker
    Provides a starting point for building a service broker that implements the Open Service Broker API.

  • Spring Cloud Cluster
    Leadership election and common stateful patterns with an abstraction and implementation for Zookeeper, Redis, Hazelcast, Consul.

  • Spring Cloud Consul
    Service discovery and configuration management with Hashicorp Consul.

  • Spring Cloud Security
    Provides support for load-balanced OAuth2 rest client and authentication header relays in a Zuul proxy.

  • Spring Cloud Sleuth
    Distributed tracing for Spring Cloud applications, compatible with Zipkin, HTrace and log-based (e.g. ELK) tracing.

  • Spring Cloud Data Flow
    A cloud-native orchestration service for composable microservice applications on modern runtimes. Easy-to-use DSL, drag-and-drop GUI, and REST-APIs together simplifies the overall orchestration of microservice based data pipelines.

  • Spring Cloud Stream
    A lightweight event-driven microservices framework to quickly build applications that can connect to external systems. Simple declarative model to send and receive messages using Apache Kafka or RabbitMQ between Spring Boot apps.

  • Spring Cloud Stream App Starters
    Spring Cloud Stream App Starters are Spring Boot based Spring Integration applications that provide integration with external systems.

  • Spring Cloud Task
    A short-lived microservices framework to quickly build applications that perform finite amounts of data processing. Simple declarative for adding both functional and non-functional features to Spring Boot apps.

  • Spring Cloud Task App Starters
    Spring Cloud Task App Starters are Spring Boot applications that may be any process including Spring Batch jobs that do not run forever, and they end/stop after a finite period of data processing.

  • Spring Cloud Zookeeper
    Service discovery and configuration management with Apache Zookeeper.

  • Spring Cloud for Amazon Web Services
    Easy integration with hosted Amazon Web Services. It offers a convenient way to interact with AWS provided services using well-known Spring idioms and APIs, such as the messaging or caching API. Developers can build their application around the hosted services without having to care about infrastructure or maintenance.

  • Spring Cloud Connectors
    Makes it easy for PaaS applications in a variety of platforms to connect to backend services like databases and message brokers (the project formerly known as "Spring Cloud").

  • Spring Cloud Starters
    Spring Boot-style starter projects to ease dependency management for consumers of Spring Cloud. (Discontinued as a project and merged with the other projects after Angel.SR2.)

  • Spring Cloud CLI
    Spring Boot CLI plugin for creating Spring Cloud component applications quickly in Groovy

  • Spring Cloud Contract
    Spring Cloud Contract is an umbrella project holding solutions that help users in successfully implementing the Consumer Driven Contracts approach.

  • Spring Cloud Gateway
    Spring Cloud Gateway is an intelligent and programmable router based on Project Reactor.

  • Spring Cloud OpenFeign
    Spring Cloud OpenFeign provides integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.

  • Spring Cloud Pipelines
    Spring Cloud Pipelines provides an opinionated deployment pipeline with steps to ensure that your application can be deployed in zero downtime fashion and easilly rolled back of something goes wrong.

Spring Cloud 的版本進化

Spring Cloud 的版本進化

按字母升序排列地梨,例如 Angel 是第一版,Brixton 是第二版缔恳。

注意:

  • Finchley 版本基于 Spring Boot 2.0.x, 不支持 Spring Boot 1.5.x.
  • Dalston 和 Edgware 版本基于 Spring Boot 1.5.x, 不支持 Spring Boot 2.0.x.
  • Camden 版本基于 Spring Boot 1.4.x, 不支持 1.5.x.
  • Brixton 和 Angel 版本已結(jié)束生命

在接下來的示例中宝剖,我們將使用 Finchley 版本的 Spring Cloud 結(jié)合 2.0.x 版本的 Spring Boot。
Finchley 版本相對于之前的版本歉甚,一些依賴的名稱發(fā)生了變化诈闺,請參考 https://spring.io/blog/2017/10/31/spring-cloud-finchley-m3-released

Finchley 版本相對于之前的版本铃芦,一些依賴的名稱發(fā)生了變化

Spring Cloud Netflix

在開始示例之前雅镊,先來了解下 Spring Cloud Netflix。
它的主要內(nèi)容是對 Netflix 公司一系列開源產(chǎn)品的包裝刃滓,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合仁烹。通過一些簡單的注解,開發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)咧虎。

它包含如下的組件和特性:

  • 服務(wù)發(fā)現(xiàn) Service Discovery: Eureka instances can be registered and clients can discover the instances using Spring-managed beans
  • 服務(wù)發(fā)現(xiàn) Service Discovery: an embedded Eureka server can be created with declarative Java configuration
  • 斷路器 Circuit Breaker: Hystrix clients can be built with a simple annotation-driven method decorator
  • 斷路器 Circuit Breaker: embedded Hystrix dashboard with declarative Java configuration
  • 聲明式的 REST 客戶端 Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations
  • 客戶端負載均衡 Client Side Load Balancer: Ribbon
  • 外部配置 External Configuration: a bridge from the Spring Environment to Archaius (enables native configuration of Netflix components using Spring Boot conventions)
  • 路由和過濾 Router and Filter: automatic regsitration of Zuul filters, and a simple convention over configuration approach to reverse proxy creation

通過 Eureka 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)

在這里我們打算建立兩個項目:

  • eureka-server:服務(wù)注冊中心
  • eureka-client:服務(wù)提供者

eureka-server 服務(wù)注冊中心

可以通過如下的 Spring Assistant 插件來創(chuàng)建項目卓缰,添加 Eureka Server 作為依賴。

eureka-server 的創(chuàng)建

eureka-server 的創(chuàng)建

eureka-server 的創(chuàng)建

pom.xml 中可以看出砰诵,導(dǎo)入了如下的依賴:

  • 2.0.3.RELEASE 版本的 Spring Boot
  • Finchley.RELEASE 版本的 Spring Cloud
  • spring-cloud-starter-netflix-eureka-server
<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-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

隨后在啟動程序中通過 @EnableEurekaServer 注解啟動一個服務(wù)注冊中心提供給其他應(yīng)用進行對話:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

隨后在 application.properties:將服務(wù)注冊中心端口設(shè)置為 1234征唬。在默認設(shè)置下,該服務(wù)注冊中心也會將自己作為客戶端來嘗試注冊它自己茁彭,所以我們需要禁用它的客戶端注冊行為总寒。

spring.application.name=eureka-server
server.port=1234

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

最后通過 mvn spring-boot:run 命令啟動項目,啟動完成后理肺,可以通過 http://127.0.0.1:1234 查看服務(wù)的注冊情況摄闸,可以看到目前暫無服務(wù)注冊到 Eureka Server。

目前暫無服務(wù)注冊到 Eureka Server

eureka-client 服務(wù)提供者

可以通過如下的 Spring Assistant 插件來創(chuàng)建項目妹萨,添加 Eureka DiscoveryWeb 作為依賴年枕。

eureka-client 的創(chuàng)建

eureka-client 的創(chuàng)建

eureka-client 的創(chuàng)建

pom.xml 中可以看出,導(dǎo)入了如下的依賴:

  • 2.0.3.RELEASE 版本的 Spring Boot
  • Finchley.RELEASE 版本的 Spring Cloud
  • spring-cloud-starter-netflix-eureka-client
  • spring-boot-starter-web
<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.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>
</dependencies>

隨后在啟動程序中通過 @EnableDiscoveryClient 來激活 Eureka 中的 DiscoveryClient 實現(xiàn):

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

隨后在 application.properties:通過 spring.application.name 屬性乎完,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時候只需要使用該名稱就可以進行服務(wù)的訪問熏兄。eureka.client.serviceUrl.defaultZone 屬性對應(yīng)服務(wù)注冊中心的配置內(nèi)容,指定服務(wù)注冊中心的位置树姨。

spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1234/eureka/

隨后摩桶,我們創(chuàng)建一個 CalculatorController 類來提供加法服務(wù):

@RestController
public class CalculatorController {

    @GetMapping("/add")
    public Integer add(@RequestParam Integer operand1, @RequestParam Integer operand2) {
        return operand1 + operand2;
    }
}

最后通過 mvn spring-boot:run 命令啟動項目,啟動完成后娃弓,可以通過 http://127.0.0.1:1234 查看服務(wù)的注冊情況典格,可以看到目前有一個服務(wù)注冊到 Eureka Server。

目前有一個服務(wù)注冊到 Eureka Server

通過 http://10.93.67.135:2001/add 可以調(diào)用該加法服務(wù):

調(diào)用該加法服務(wù)

如果我們將端口修改為 2002台丛,重新打開一個 terminal 啟動項目耍缴,可以看到該服務(wù)對應(yīng)了兩個實例 Zones。


該服務(wù)對應(yīng)了兩個實例 Zones

通過 Consul 實現(xiàn)服務(wù)注冊發(fā)現(xiàn)

Spring Cloud Consul項目是針對Consul的服務(wù)治理實現(xiàn)挽霉。Consul是一個分布式高可用的系統(tǒng)防嗡,它包含多個組件,但是作為一個整體侠坎,在微服務(wù)架構(gòu)中為我們的基礎(chǔ)設(shè)施提供服務(wù)發(fā)現(xiàn)和服務(wù)配置的工具蚁趁。

此處不詳細介紹,參見 Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊與發(fā)現(xiàn)(Eureka实胸、Consul)【Dalston版】


引用:
程序猿DD Spring Cloud基礎(chǔ)教程
Spring Cloud構(gòu)建微服務(wù)架構(gòu):服務(wù)注冊與發(fā)現(xiàn)(Eureka他嫡、Consul)【Dalston版】
Spring Cloud Dalston中文文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末番官,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子钢属,更是在濱河造成了極大的恐慌徘熔,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淆党,死亡現(xiàn)場離奇詭異酷师,居然都是意外死亡,警方通過查閱死者的電腦和手機染乌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門山孔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人荷憋,你說我怎么就攤上這事台颠。” “怎么了台谊?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵蓉媳,是天一觀的道長。 經(jīng)常有香客問我锅铅,道長酪呻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任盐须,我火速辦了婚禮玩荠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘贼邓。我一直安慰自己阶冈,他們只是感情好,可當我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布塑径。 她就那樣靜靜地躺著女坑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪统舀。 梳的紋絲不亂的頭發(fā)上匆骗,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機與錄音誉简,去河邊找鬼碉就。 笑死,一個胖子當著我的面吹牛闷串,可吹牛的內(nèi)容都是我干的瓮钥。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼碉熄!你這毒婦竟也來了桨武?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤具被,失蹤者是張志新(化名)和其女友劉穎玻募,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體一姿,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年跃惫,在試婚紗的時候發(fā)現(xiàn)自己被綠了叮叹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡爆存,死狀恐怖蛉顽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情先较,我是刑警寧澤携冤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站闲勺,受9級特大地震影響曾棕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜菜循,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一翘地、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧癌幕,春花似錦衙耕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至胶逢,卻和暖如春厅瞎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宪塔。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工磁奖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人某筐。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓比搭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子身诺,可洞房花燭夜當晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容