ribbon+rest怎么使用

本篇內(nèi)容主要講解“ribbon+rest怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ribbon+rest怎么使用”吧!

創(chuàng)新互聯(lián)建站長期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為樺南企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,樺南網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

在微服務(wù)架構(gòu)中,業(yè)務(wù)都會被拆分成一個獨立的服務(wù),服務(wù)與服務(wù)的通訊是基于http restful的。Spring cloud有兩種服務(wù)調(diào)用方式,一種是ribbon+restTemplate,另一種是feign。

一、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是一個負載均衡客戶端,可以很好的控制htt和tcp的一些行為。Feign默認集成了ribbon。

ribbon 已經(jīng)默認實現(xiàn)了這些配置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,并啟動,了解springcloud架構(gòu)可以加求求:三五三六二四七二五九,這時你會發(fā)現(xiàn):

service-hi在eureka-server注冊了2個實例,這就相當于一個小的集群。

ribbon+rest怎么使用

三、建一個服務(wù)消費者
重新新建一個spring-boot工程,取名為:service-ribbon;
在它的pom.xml繼承了父pom文件,并引入了以下依賴:

<?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>com.forezp</groupId>
        <artifactId>sc-f-chapter2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>
 
</project>

在工程的配置文件指定服務(wù)的注冊中心地址為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向服務(wù)中心注冊;并且向程序的ioc注入一個bean: restTemplate;并通

過@LoadBalanced注解表明這個restRemplate開啟負載均衡的功能。

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

寫一個測試類HelloService,通過之前注入ioc容器的restTemplate來消費service-hi服務(wù)的“/hi”接口,在這里我們直接

用的程序名替代了具體的url地址,在ribbon中它會根據(jù)服務(wù)名來選擇具體的服務(wù)實例,根據(jù)服務(wù)實例在請求的時候會用具

體的url替換掉服務(wù)名,代碼如下:

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

寫一個controller,在controller中用調(diào)用HelloService 的方法,代碼如下:

@RestController
public class HelloControler {
    @Autowired
    HelloService helloService;
    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService( name );
    }
}

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

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

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

四、此時的架構(gòu)

ribbon+rest怎么使用

  • 一個服務(wù)注冊中心,eureka server,端口為8761

  • service-hi工程跑了兩個實例,端口分別為8762,8763,分別向服務(wù)注冊中心注冊

  • sercvice-ribbon端口為8764,向服務(wù)注冊中心注冊

  • 當sercvice-ribbon通過restTemplate調(diào)用service-hi的hi接口時,因為用ribbon進行了負載均衡,會輪流的調(diào)用service-hi:8762和8763 兩個端口的hi接口.

到此,相信大家對“ribbon+rest怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

本文名稱:ribbon+rest怎么使用
URL地址:http://muchs.cn/article26/ighccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗移動網(wǎng)站建設(shè)、網(wǎng)站收錄、云服務(wù)器、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)