java版b2b2c社交電商spring cloud分布式微服務(二) 服務消費者(rest+ribbon)

一、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.

—–摘自官網

ribbon是一個負載均衡客戶端,可以很好的控制htt和tcp的一些行為。Feign默認集成了ribbon。

ribbon 已經默認實現了這些配置bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、準備工作

這一篇文章基于上一篇文章的工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8762;將service-hi的配置文件的端口改為8763,并啟動,這時你會發現:service-hi在eureka-server注冊了2個實例,這就相當于一個小的集群。訪問localhost:8761如圖所示:

三、建一個服務消費者

重新新建一個spring-boot工程,取名為:service-ribbon;?
在它的pom.xml文件分別引入起步依賴spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代碼如下:

<?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>com.forezp</groupId><artifactId>service-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-ribbon</name><description>Demo project for Spring Boot</description><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><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><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.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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>Dalston.RC1</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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

在工程的配置文件指定服務的注冊中心地址為http://localhost:8761/eureka/,程序名稱為 service-ribbon,程序端口為8764。配置文件application.yml如下:

eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
server:port: 8764
spring:application:name: service-ribbon

在工程的啟動類中,通過@EnableDiscoveryClient向服務中心注冊;并且向程序的ioc注入一個bean: restTemplate;并通過@LoadBalanced注解表明這個restRemplate開啟負載均衡的功能。

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}}

寫一個測試類HelloService,通過之前注入ioc容器的restTemplate來消費service-hi服務的“/hi”接口,在這里我們直接用的程序名替代了具體的url地址,在ribbon中它會根據服務名來選擇具體的服務實例,根據服務實例在請求的時候會用具體的url替換掉服務名,代碼如下:

@Service
public class HelloService {@AutowiredRestTemplate restTemplate;public String hiService(String name) {return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);}}

寫一個controller,在controller中用調用HelloService 的方法,代碼如下:

/*** Created by fangzhipeng on 2017/4/6.*/
@RestController
public class HelloControler {@AutowiredHelloService helloService;@RequestMapping(value = "/hi")public String hi(@RequestParam String name){return helloService.hiService(name);}}

在瀏覽器上多次訪問http://localhost:8764/hi?name=forezp,瀏覽器交替顯示:

hi forezp,i am from port:8762hi forezp,i am from port:8763

 這說明當我們通過調用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法時,已經做了負載均衡,訪問了不同的端口的服務實例。

代碼架構如下:

需要JAVASpring Cloud大型企業分布式微服務云構建的B2B2C電子商務社交平臺源碼請加企鵝求求:一零三八七七四六二六

轉載于:https://www.cnblogs.com/sunnysunny/p/10819551.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/251820.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/251820.shtml
英文地址,請注明出處:http://en.pswp.cn/news/251820.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[學習筆記]支配樹

被支配樹支配的恐懼 定義 顯然&#xff0c;這個支配關系是一個樹&#xff08;或者如果有的點不能從r到達&#xff0c;就是一個樹一堆點&#xff09;。 首先不會成環&#xff0c;其次也不會是DAG 即如果A支配C&#xff0c;B支配C&#xff0c;那么A和B之間必然有支配關系 解法 首…

RBAC 權限設計(轉載)

來源 &#xff1a;https://blog.csdn.net/rocher88/article/details/43190743 這是我在網上找的一些設計比較好的RBAC權限管理不知道&#xff0c;像新浪、搜狐、網易、百度、阿里巴巴、淘寶網的RBAC用戶權限這一塊&#xff0c;都是這種細顆粒的RBAC設計開發&#xff0c;還是把他…

54.get set

當程序查詢對象屬性時調用get方法,如果只有get方法那么他是一個只讀屬性&#xff0c;//程序對對象屬性進行賦值操作時調用set方法&#xff0c;如果只有set方法那么他是是一個只讀屬性 <script type"text/javascript">var p {x:1.0,y:1.0,//當程序查詢對象屬性…

Codeforces Round #554 Div.2 E - Neko and Flashback

歐拉路徑 神題啊神題&#xff01;這道題的突破口就是后兩個數組每個元素是一一對應的。 也就是說&#xff0c;對于一個p的排列&#xff0c;b和c取得每一個元素的下標在p中都是一樣的。 根據b和c數組的性質可以得出&#xff0c;b[i] < c[i]。 這也是我們輸出-1的一個判斷方法…

20172311 2017-2018-2 《程序設計與數據結構》第八周學習總結

20172311 2017-2018-2 《程序設計與數據結構》第八周學習總結 教材學習內容總結 本周對JAVA中的多態性進行了學習 多態性引用能夠隨時間變化指向不同類型的對象&#xff0c;是通過后綁定實現的。實現多態性的主要途徑有兩種&#xff1a; 1.由繼承實現多態性 2.利用接口實現多態…

Linux系統安裝Apache 2.4.6

http://www.cnblogs.com/kerrycode/p/3261101.html Apache簡介 Apache HTTP Server&#xff08;簡稱Apache&#xff09;是Apache軟件基金會的一個開放源碼的網頁服務器&#xff0c;可以在大多數計算機操作系統中運行&#xff0c;由于其多平臺和安全性被廣泛使用&#xff0c;是最…

深淺拷貝

lst1 ["金毛獅王", "紫衫龍王", "白眉鷹王", "青翼蝠王"] lst2 lst1 print(lst1) print(lst2) lst1.append("楊逍") print(lst1) print(lst2) # 結果: # [金毛獅王, 紫衫龍王, 白眉鷹王, 青翼蝠王, 楊逍] # [金毛獅王 紫衫…

lnmp化境開啟pathinfo,支持tp5.0等訪問

一、 開啟pathinfo   #注釋 下面這一行 #include enable-php.conf #載入新的配置文件 include enable-php-pathinfo.conf #添加如下location / {if (!-e $request_filename){rewrite ^/(.*)$ /index.php/$1 last;break;}}location ~ /index.php {fastcgi_pass 127.0.0.1:…

深度解密GO語言之反射

反射和 Interface 息息相關&#xff0c;而 Interface 是我們上一篇文章的內容。在開始正文前&#xff0c;和大家說點題外話。 上一篇關于 Interface 的文章發出后&#xff0c;獲得了很多的關注和閱讀。比如&#xff0c;登上了 GoCN 的每日新聞第一條&#xff1a; 可能是編輯者覺…

Python爬蟲-正則表達式

正則表達式 只提取關注的數據&#xff0c;進行數據賽選 原子&#xff1a; 基本組成單位 普通的字符 非打印支付 通用字符 普通的字符 >>> import re >>> pat"yue" >>> string"http://yum.iqianyue.com" >>> rst1re.se…

openfire(一):使用idea編譯openfire4.2.3源碼

最近公司項目要使用openfire&#xff0c;并對源碼做一些修改&#xff0c;使用的openfire版本為官網目前最新版本4.2.3&#xff0c;網上資料較少&#xff0c;踩了很多坑&#xff0c;特此記錄。 1.下載源碼 http://www.igniterealtime.org/downloads/source.jsp 2.使用idea導入源…

JAVA synchronized關鍵字鎖機制(中)

synchronized 鎖機制簡單的用法&#xff0c;高效的執行效率使成為解決線程安全的首選。 下面總結其特性以及使用技巧&#xff0c;加深對其理解。 特性: 1. Java語言的關鍵字&#xff0c;當它用來修飾一個方法或者一個代碼塊的時候&#xff0c;能夠保證在同一時刻最多只有一個線…

Python多線程豆瓣影評API接口爬蟲

爬蟲庫 使用簡單的requests庫&#xff0c;這是一個阻塞的庫&#xff0c;速度比較慢。 解析使用XPATH表達式 總體采用類的形式 多線程 使用concurrent.future并發模塊&#xff0c;建立線程池&#xff0c;把future對象扔進去執行即可實現并發爬取效果 數據存儲 使用Python ORM sq…

【自制工具類】Java刪除字符串中的元素

這幾天做項目需要把多個item的id存儲到一個字符串中&#xff0c;保存進數據庫。保存倒是簡單&#xff0c;只需要判斷之前是否為空&#xff0c;如果空就直接添加&#xff0c;非空則拼接個“&#xff0c;” 所以這個字符串的數據結構是這樣的 String str "a,b,c,d"; 保…

DMA存儲器到外設代碼講解

實驗目的: bsp_dma_mtp.h #ifndef __BSP_DMA_MTP_H #define __BSP_DMA_MTP_H#include "stm32f10x.h" #include <stdio.h>// 串口工作參數宏定義 #define DEBUG_USARTx USART1 #define DEBUG_USART_CLK RCC_APB2Periph_USAR…

java基礎集合類——LinkedList 源碼略讀

1.概覽 LinkedList是java的動態數組另一種實現方式&#xff0c;底層是基于雙向鏈表&#xff0c;而不是數組。 public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, java.io.Serializable LinkedLis…

[BZOJ] 1688: [Usaco2005 Open]Disease Manangement 疾病管理

1688: [Usaco2005 Open]Disease Manangement 疾病管理 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 727 Solved: 468[Submit][Status][Discuss]Description Alas! A set of D (1 < D < 15) diseases (numbered 1..D) is running through the farm. Farmer John woul…

es6 var、let、const命令

1.let和var <1>let聲明的變量僅在塊級作用域內有效&#xff1b; var聲明的變量在全局有效&#xff1b; <2> var變量樂意在聲明之前使用&#xff0c;輸出undefined; let 不可以&#xff0c;直接拋出一個錯誤&#xff1b; 例如&#xff1a;//var 聲明console.log(a);…

實例屬性和類屬

1.Python是動態語言&#xff0c;根據類創建的實例&#xff0c;可以任意綁定屬性 2.給實例綁定屬性的方法有兩種&#xff1a; 通過實例變量或者通過self變量。 1 class Student(object): 2 def __init__(self, name): 3 self.namename 4 5 ##或者如下&#xff1a; 6 &g…

vim中跳到第一行和最后一行

底線命令模式 :0或:1跳到文件第一行 :$跳到文件最后一行 命令模式 gg跳到第一行 shiftg跳到文件最后一行轉載于:https://www.cnblogs.com/liuys635/p/10831196.html