摘要:在服務(wù)架構(gòu)中,業(yè)務(wù)都會被拆分成一個獨立的服務(wù),服務(wù)與服務(wù)的通訊是基于的。配置文件如下在工程的啟動類中通過向服務(wù)中心注冊并且注冊了一個通過注冊表明,這個是負(fù)載均衡的。
轉(zhuǎn)載請標(biāo)明出處:
http://blog.csdn.net/forezp/a...
本文出自方志朋的博客
在上一篇文章,講了服務(wù)的注冊和發(fā)現(xiàn)。在服務(wù)架構(gòu)中,業(yè)務(wù)都會被拆分成一個獨立的服務(wù),服務(wù)與服務(wù)的通訊是基于http restful的。Spring cloud有兩種調(diào)用方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基于ribbon+rest。
一、ribbon簡介Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
-----摘自官網(wǎng)
ribbon是一個負(fù)載均衡客戶端,可以很好的控制htt和tcp的一些行為。Feign也用到ribbon,當(dāng)你使用@ FeignClient,ribbon自動被應(yīng)用。
ribbon 已經(jīng)默認(rèn)實現(xiàn)了這些配置bean:
IClientConfig ribbonClientConfig: DefaultClientConfigImpl
IRule ribbonRule: ZoneAvoidanceRule
IPing ribbonPing: NoOpPing
ServerList
ServerListFilter
ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
二、準(zhǔn)備工作基于上一節(jié)的工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8762;將service-hi的配置文件的端口改為8763,并啟動它,這時你會發(fā)現(xiàn):service-hi在eureka-server注冊了2個,這就相當(dāng)于一個小的集群。訪問localhost:8761如圖所示:
三、建一個服務(wù)消費者重新新建一個spring-boot工程,取名為:service-ribbon;
它的pom.xml文件如下:
4.0.0 com.forezp service-ribbon 0.0.1-SNAPSHOT jar service-ribbon Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-ribbon org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Dalston.RC1 pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
向服務(wù)注冊中心注冊一個新的服務(wù),這時service-ribbon既是服務(wù)提供者,也是服務(wù)消費者。配置文件application.yml如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon
在工程的啟動類中,通過@EnableDiscoveryClient向服務(wù)中心注冊;并且注冊了一個bean: restTemplate;通過@ LoadBalanced注冊表明,這個restRemplate是負(fù)載均衡的。
@SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
這時我們需要測試下,建一個service類:
@Service public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name) { return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); } }
通過restTemplate.getForObject方法,service-ribbon 就可以調(diào)用service-hi的方法了。并且在調(diào)用的工程中并之需要寫服務(wù)的名,而不是具體的ip.
寫一個controller:
/** * Created by fangzhipeng on 2017/4/6. */ @RestController public class HelloControler { @Autowired HelloService helloService; @RequestMapping(value = "/hi") public String hi(@RequestParam String name){ return helloService.hiService(name); } }
訪問http://localhost:8764/hi?name...瀏覽器交替顯示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
這說明當(dāng)我們通過調(diào)用restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class),獲取service-hi的方法時,已經(jīng)做了負(fù)載均衡,訪問了不同的端口的服務(wù)。
四、此時的架構(gòu)一個服務(wù)注冊中心,eureka server,端口為8761
service-hi工程跑了兩個副本,端口分別為8762,8763,分別向服務(wù)注冊中心注冊
sercvice-ribbon端口為8764,向服務(wù)注冊中心注冊
當(dāng)sercvice-ribbon通過restTemplate調(diào)用service-hi的hi接口時,因為用ribbon進(jìn)行了負(fù)載均衡,會輪流的調(diào)用service-hi:8762和8763 兩個端口的hi接口;
源碼下載:https://github.com/forezp/SpringCloudLearning/tree/master/chapter2
五、參考資料本文參考了以下:
spring-cloud-ribbon
springcloud ribbon with eureka
服務(wù)消費者
優(yōu)秀文章推薦:史上最簡單的 SpringCloud 教程 | 終章
史上最簡單的 SpringCloud 教程 | 第一篇: 服務(wù)的注冊與發(fā)現(xiàn)(Eureka)
史上最簡單的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69927.html
摘要:接下來繼續(xù)介紹三種架構(gòu)模式,分別是查詢分離模式微服務(wù)模式多級緩存模式。分布式應(yīng)用程序可以基于實現(xiàn)諸如數(shù)據(jù)發(fā)布訂閱負(fù)載均衡命名服務(wù)分布式協(xié)調(diào)通知集群管理選舉分布式鎖和分布式隊列等功能。 SpringCloud 分布式配置 SpringCloud 分布式配置 史上最簡單的 SpringCloud 教程 | 第九篇: 服務(wù)鏈路追蹤 (Spring Cloud Sleuth) 史上最簡單的 S...
摘要:它就是史上最簡單的教程第三篇服務(wù)消費者后端掘金上一篇文章,講述了通過去消費服務(wù),這篇文章主要講述通過去消費服務(wù)。概覽和架構(gòu)設(shè)計掘金技術(shù)征文后端掘金是基于的一整套實現(xiàn)微服務(wù)的框架。 Spring Boot 配置文件 – 在坑中實踐 - 后端 - 掘金作者:泥瓦匠鏈接:Spring Boot 配置文件 – 在坑中實踐版權(quán)歸作者所有,轉(zhuǎn)載請注明出處本文提綱一、自動配置二、自定義屬性三、ran...
摘要:一簡介是一個聲明式的服務(wù)客戶端,它使得寫服務(wù)變得更簡單。同時支持可插拔的編碼器和解碼器。對添加了支持,同時在中次用相同的。 轉(zhuǎn)載請標(biāo)明出處: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 上一篇文章,講述了通過restTemplate+ribbon去消費服務(wù),這篇文章主要講述通過feign去消費服務(wù)。 一、Feign簡介 Feign是一個聲明式的...
摘要:為了保證其高可用,單個服務(wù)又必須集群部署。為了解決這個問題,就出現(xiàn)斷路器模型。一斷路器簡介摘自官網(wǎng)已經(jīng)創(chuàng)建了一個名為的庫來實現(xiàn)斷路器模式。較底層的服務(wù)如果出現(xiàn)故障,會導(dǎo)致連鎖故障。當(dāng)對特定的服務(wù)的調(diào)用達(dá)到一個閥值是秒次斷路器將會被打開。 轉(zhuǎn)載請標(biāo)明出處: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 在微服務(wù)架構(gòu)中,我們將業(yè)務(wù)拆分成一個個的服務(wù),...
閱讀 1018·2023-04-26 02:56
閱讀 14590·2021-11-23 09:51
閱讀 1916·2021-09-26 10:14
閱讀 3022·2019-08-29 13:09
閱讀 2182·2019-08-26 13:29
閱讀 596·2019-08-26 12:02
閱讀 3594·2019-08-26 10:42
閱讀 3030·2019-08-23 18:18