Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例)
說明:
ribbon是spring-cloud中作為服務(wù)消費(fèi)者的一種角色,客戶端可以通過它來對服務(wù)提供者的服務(wù)進(jìn)行消費(fèi),
比如本例中是服務(wù)提供者注冊到注冊中心,服務(wù)提供者提供了一個(gè)服務(wù)接口,返回一個(gè)hello字符串,我們通過ribbon將這個(gè)接口調(diào)用,再不暴露真實(shí)服務(wù)提供者的地址的同時(shí),獲取服務(wù)提供者的服務(wù)
前提:
按照之前幾個(gè)教程,搭建出注冊中心、服務(wù)提供者。這里可以使用分片的注冊中心,也可以不使用,這里暫時(shí)定為使用之前搭好的分片注冊中心,服務(wù)提供者僅提供一個(gè)即可。
準(zhǔn)備工作:
1、啟動(dòng)注冊中心
按照之前的教程,分別使用peer1和peer2進(jìn)行啟動(dòng)兩個(gè)分片的注冊中心,如果是單節(jié)點(diǎn)直接啟動(dòng)項(xiàng)目即可

啟動(dòng)后可以查看下localhost:1111或localhost:1112,如圖


2、啟動(dòng)服務(wù)提供者
這里為了查看負(fù)載均衡的情況,所以需要啟動(dòng)兩個(gè)服務(wù)提供者
按照之前教程中,分別開啟兩個(gè)terminal進(jìn)行指定端口啟動(dòng)(兩個(gè)相同的服務(wù)也可以在各自的配置文件中配置不同的端口),兩個(gè)終端指令分別如下:
cd target java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080
cd target java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081
啟動(dòng)結(jié)果:

至此,準(zhǔn)備工作已經(jīng)完成
正文:
1、ribbon服務(wù)搭建
新建一個(gè)maven項(xiàng)目,不使用模板即可。項(xiàng)目名為robbin-customer,導(dǎo)入依賴參考如下:
<?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.hellxz</groupId>
<artifactId>ribbon-customer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
新建springboot啟動(dòng)類,將RestTemplate交給Spring容器進(jìn)行管理
package com.cnblogs.hellxz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @Author : Hellxz
* @Description: 消費(fèi)應(yīng)用
* @Date : 2018/4/16 15:45
*/
@EnableDiscoveryClient
@SpringBootApplication
public class CustomerApplication {
@Bean //將此Bean交給spring容器
@LoadBalanced //通過此注解開啟負(fù)載均衡
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
在src/resources目錄下創(chuàng)建一個(gè)application.yml進(jìn)行配置注冊信息相關(guān),本文使用了分片的注冊中心,單節(jié)點(diǎn)請配置一個(gè)defaltZone即可
server: port: 9000 #為ribbon-customer指定服務(wù)端口 spring: application: name: ribbon-customer #指定應(yīng)用名 #指定eureka注冊中心地址 eureka: client: serviceUrl: defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
在啟動(dòng)類同級目錄創(chuàng)建CustomerController,注入RestTemplate進(jìn)行調(diào)用服務(wù)接口
package com.cnblogs.hellxz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author : Hellxz
* @Description: 消費(fèi)者應(yīng)用
* @Date : 2018/4/16 15:54
*/
@RestController
public class CustomerController {
@Autowired
//注入restTemplate
private RestTemplate restTemplate;
@RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)
public String helloCustomer(){
//這里注釋掉,因?yàn)橹跋氘?dāng)然使用了直鏈訪問服務(wù)提供者的接口,這樣是不會返回結(jié)果的,而且會報(bào)錯(cuò)
//return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody();
//使用restTemplate調(diào)用微服務(wù)接口
return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
}
}
注意:上述代碼第24行給出了一個(gè)錯(cuò)誤的演示,如果出現(xiàn)訪問ribbon接口報(bào)錯(cuò),出現(xiàn)白色報(bào)錯(cuò)頁面,請檢查這里
至此ribbon消費(fèi)者應(yīng)用搭建完成,啟動(dòng)測試
測試:
訪問http://localhost:1111/ 我們發(fā)現(xiàn)這個(gè)ribbon消費(fèi)者應(yīng)用已經(jīng)注冊到注冊中心中了

訪問http://localhost:9000/ribbon-customer

還記得服務(wù)提供者項(xiàng)目中的如圖方法中如果有訪問則會打印信息,因?yàn)閱?dòng)了兩個(gè)服務(wù)提供者,這里可以測試ribbon的負(fù)載均衡

查看服務(wù)提供者輸出

而第二個(gè)終端沒有,依舊顯示到Resolving eureka endpoints via configuration這行
刷新頁面查看終端,由于ribbon的默認(rèn)負(fù)載均衡的實(shí)現(xiàn)是輪詢的,所以可能出現(xiàn)多次訪問同一服務(wù),多刷新幾次頁面,一定會在另一個(gè)Termical中顯示的!

結(jié)語:
Ribbon作為服務(wù)消費(fèi)者,可以在用戶獲取到服務(wù)提供者提供的服務(wù)的同時(shí),不向用戶暴露接口地址??梢钥吹?,這里調(diào)用服務(wù)接口的時(shí)候使用的是服務(wù)提供者的服務(wù)名代替主機(jī)名,這在服務(wù)治理框架中,這種特性很重要。
由于本人之前是先學(xué)的jhipster生成器,所以,提前預(yù)報(bào)一下,之后還會有一種名為Feign的技術(shù)可以替代Ribbon,本系列博客均為學(xué)習(xí)筆記,實(shí)際操作中,可能會存在一個(gè)應(yīng)用既是服務(wù)提供者又是服務(wù)消費(fèi)者的情況。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解spring security 配置多個(gè)AuthenticationProvider
這篇文章主要介紹了詳解spring security 配置多個(gè)AuthenticationProvider ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Spring實(shí)戰(zhàn)之緩存使用key操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用key操作,結(jié)合實(shí)例形式分析了Spring緩存使用key具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析,有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù),MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能,需要的朋友可以參考下2023-11-11
Java編程實(shí)現(xiàn)獲取mp3時(shí)長及播放mp3文件的方法
這篇文章主要介紹了Java編程實(shí)現(xiàn)獲取mp3時(shí)長及播放mp3文件的方法,涉及java基于jaudiotagger與jl包對MP3音頻文件屬性操作及音頻播放相關(guān)操作技巧,并提供了相關(guān)jar包的本站下載,需要的朋友可以參考下2018-02-02
淺析Java中SimpleDateFormat為什么是線程不安全的
SimpleDateFormat是Java中用于日期時(shí)間格式化的一個(gè)類,它提供了對日期的解析和格式化能力,本文主要來和大家一起探討一下SimpleDateFormat為什么是線程不安全的,感興趣的可以了解下2024-02-02

