Spring Cloud 5分鐘搭建教程

1.前言:git

1.1.如下內容是我經過閱讀官方文檔,併成功實踐後的經驗總結,但願能幫助你更快地理解和使用Spring Cloud.github

1.2.默認讀者已經熟練掌握Spring 全家桶,Spring Boot和註解開發.spring

1.3.陸續更新apache

2.開發環境: @Deprecated瀏覽器

2.1.開發工具:idea架構

2.2.開發環境:jdk1.7負載均衡

2.3.Spring版本:maven

2.3.1.Spring Boot :1.4.0 release分佈式

2.3.2.Spring Cloud : Camden SR2ide

3.demo:(獻給急於速成的各位大兄弟): demo地址: https://github.com/leoChaoGlut/spring-cloud-demo

3.1.服務註冊demo:

Spring Cloud 5分鐘搭建教程

 

3.1.1.建立工程模塊,如圖所示

3.1.2.將官方提供的maven依賴,加入pom.

若是想學習Java工程化、高性能及分佈式、深刻淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友能夠加個人Java高級交流:787707172,羣裏有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給你們。

<?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>demo</groupId>
 <artifactId>spring-cloud-demo</artifactId>
 <packaging>pom</packaging>
 <version>1.0-SNAPSHOT</version>
 
 <modules>
 <module>discovery</module>
 <module>service0</module>
 <module>service1</module>
 </modules>
 
 <!--如下dependency來自官方-->
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.0.RELEASE</version>
 </parent>
 
 <dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-dependencies</artifactId>
 <version>Camden.SR2</version>
 <type>pom</type>
 <scope>import</scope>
 </dependency>
 </dependencies>
 </dependencyManagement>
 
 <dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-eureka</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <optional>true</optional>
 </dependency>
 </dependencies>
 
 
</project>

3.1.3.如圖步驟,完成Discovery

Spring Cloud 5分鐘搭建教程

 

3.1.4.如圖步驟完成Service0,Service1相似

Spring Cloud 5分鐘搭建教程

 

3.1.5.簡單到爆炸有沒有...........,接下來先啓動Discovery,而後啓動Service0和Service1

3.1.6.打開瀏覽器,訪問 localhost:8080 ,8080是Discovery裏配置的端口號.一切順利的話,能夠看到:

Spring Cloud 5分鐘搭建教程

 

3.1.7.已經成功註冊了service0,service1兩個服務

3.2.網關demo: 光是註冊了服務還不行,這裏能夠再配一個網關,讓服務調用有統一的入口.

Spring Cloud 5分鐘搭建教程

 

3.2.1.經過上圖配置後,首先啓動Discovery,其次的服務和網關啓動順序隨意.經過訪問localhost:8083/service0/service0,便可看到,gateway幫咱們轉發了請求.

3.3.Feign:一個能夠把遠程服務提供方的 rest 接口變成本地方法調用的Spring Cloud組件

舉個栗子:

如今有2個服務,service0, service1

service0提供了一個test接口,

那麼這時候,若是service1須要的調用service0,除了經過網關(zuul)調用,還可使用Feign,來把service0的遠程接口,變爲本地方法調用.如圖:

Spring Cloud 5分鐘搭建教程

 

4.feign + ribbon + hystrix

簡介:

hystrix: 以切面爲原理,能夠在不入侵業務代碼的狀況下,給方法加上超時等指標,而且能夠在超出設置的指標後,調用指定的fallback方法,進行失敗回調處理.

ribbon: 客戶端負載均衡, 我曾經也寫了一個相似的東西(https://github.com/leoChaoGlut/ServiceDIscoveryAndRegistry/tree/master/doc),不事後來發現spring cloud已經有成熟的,現成的經常使用組件,因此就放棄了.哈哈.... 老式的,無註冊中心的服務調用,是經過url來實現的,可是ribbon可讓咱們只須要提供服務名,就能夠調用到多實例的服務,而且在客戶端作一個負載分發,減輕服務端負載的壓力.

feign: 給你以Http的形式,帶來RPC般的體驗.

認真看圖和代碼,便可快速上手 feign + ribbon + hystrix 配置

Spring Cloud 5分鐘搭建教程

 

細看spring cloud, feign,ribbon,hystrix的官方文檔,加上源碼的閱讀,便可掌握如何使用spring cloud 配置 這三個組件.

歡迎工做一到八年的Java工程師朋友們加入Java高級交流:787707172

本羣提供免費的學習指導 架構資料 以及免費的解答

不懂得問題均可以在本羣提出來 以後還會有直播平臺和講師直接交流噢

相關文章
相關標籤/搜索