SpringCloud(四)服務發現與消費:以ribbon為例


說明:

ribbon是spring-cloud中作為服務消費者的一種角色,客戶端可以通過它來對服務提供者的服務進行消費,

比如本例中是服務提供者注冊到注冊中心,服務提供者提供了一個服務接口,返回一個hello字符串,我們通過ribbon將這個接口調用,再不暴露真實服務提供者的地址的同時,獲取服務提供者的服務

前提:

按照之前幾個教程,搭建出注冊中心、服務提供者。這里可以使用分片的注冊中心,也可以不使用,這里暫時定為使用之前搭好的分片注冊中心,服務提供者僅提供一個即可。

准備工作:

1、啟動注冊中心

按照之前的教程,分別使用peer1和peer2進行啟動兩個分片的注冊中心,如果是單節點直接啟動項目即可

啟動后可以查看下localhost:1111或localhost:1112,如圖

2、啟動服務提供者

這里為了查看負載均衡的情況,所以需要啟動兩個服務提供者

按照之前教程中,分別開啟兩個terminal進行指定端口啟動(兩個相同的服務也可以在各自的配置文件中配置不同的端口),兩個終端指令分別如下:

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

啟動結果:

 

至此,准備工作已經完成

正文:

1、ribbon服務搭建

新建一個maven項目,不使用模板即可。項目名為robbin-customer,導入依賴參考如下:

<?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啟動類,將RestTemplate交給Spring容器進行管理

 1 package com.cnblogs.hellxz;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 /**
11  * @Author : Hellxz
12  * @Description: 消費應用
13  * @Date : 2018/4/16 15:45
14  */
15 @EnableDiscoveryClient
16 @SpringBootApplication
17 public class CustomerApplication {
18 
19     @Bean  //將此Bean交給spring容器
20     @LoadBalanced  //通過此注解開啟負載均衡
21     RestTemplate restTemplate(){
22         return new RestTemplate();
23     }
24 
25     public static void main(String[] args) {
26         SpringApplication.run(CustomerApplication.class, args);
27     }
28 }

在src/resources目錄下創建一個application.yml進行配置注冊信息相關,本文使用了分片的注冊中心,單節點請配置一個defaltZone即可

server:
  port: 9000   #為ribbon-customer指定服務端口

spring:
  application:
    name: ribbon-customer  #指定應用名

#指定eureka注冊中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

在啟動類同級目錄創建CustomerController,注入RestTemplate進行調用服務接口

 1 package com.cnblogs.hellxz;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 /**
10  * @Author : Hellxz
11  * @Description: 消費者應用
12  * @Date : 2018/4/16 15:54
13  */
14 @RestController
15 public class CustomerController {
16 
17     @Autowired
18     //注入restTemplate
19     private RestTemplate restTemplate;
20 
21     @RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)
22     public String helloCustomer(){
23         //這里注釋掉,因為之前想當然使用了直鏈訪問服務提供者的接口,這樣是不會返回結果的,而且會報錯
24         //return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody();
25         //使用restTemplate調用微服務接口
26         return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
27 
28     }
29 }

注意:上述代碼第24行給出了一個錯誤的演示,如果出現訪問ribbon接口報錯,出現白色報錯頁面,請檢查這里

至此ribbon消費者應用搭建完成,啟動測試

測試:

訪問http://localhost:1111/    我們發現這個ribbon消費者應用已經注冊到注冊中心中了

 

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

還記得服務提供者項目中的如圖方法中如果有訪問則會打印信息,因為啟動了兩個服務提供者,這里可以測試ribbon的負載均衡

查看服務提供者輸出

而第二個終端沒有,依舊顯示到Resolving eureka endpoints via configuration這行

刷新頁面查看終端,由於ribbon的默認負載均衡的實現是輪詢的,所以可能出現多次訪問同一服務,多刷新幾次頁面,一定會在另一個Termical中顯示的!

結語:

Ribbon作為服務消費者,可以在用戶獲取到服務提供者提供的服務的同時,不向用戶暴露接口地址。可以看到,這里調用服務接口的時候使用的是服務提供者的服務名代替主機名,這在服務治理框架中,這種特性很重要。

由於本人之前是先學的jhipster生成器,所以,提前預報一下,之后還會有一種名為Feign的技術可以替代Ribbon,本系列博客均為學習筆記,實際操作中,可能會存在一個應用既是服務提供者又是服務消費者的情況。

 

聲明本文為本人學習筆記,如需轉載,還請注明出處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM