SpringCloud之Ribbon
admin
2023-02-21 03:20:05
0

前面的话】书接上文,本文的某些知识依赖我的上一篇文章:SpringCloud之Eureka,如果没有看过可以先移步去看一下。另外在微服务架构中,业务都会被拆分成一个个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。


壹、Ribbon简介

  • 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


# 贰、准备工作
- 新建一个ribbon子工程**lovin-ribbon-client**,用于后面的操作。下面是主要的pom依赖

~~~pom

        lovincloud
        com.eelve.lovincloud
        1.0-SNAPSHOT
    
    4.0.0

    lovin-ribbon-client
    jar
    ribbonclient
    0.0.1
    ribbon的client

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
  • 这里为了安全,我这里还是添加spring-boot-starter-security
    server:
    port: 8805   # 服务端口号
    spring:
    application:
    name: lovinribbonclient     # 服务名称
    security:
    basic:
      enabled: true
    user:
      name: lovin
      password: ${REGISTRY_SERVER_PASSWORD:lovin}
    eureka:
    client:
    serviceUrl:
      defaultZone: http://lovin:lovin@localhost:8881/eureka/   # 注册到的eureka服务地址
  • 配置spring-boot-starter-security,这里为了方便我这里放开所有请求
    
    package com.eelve.lovin.cofig;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**

  • @ClassName SecurityConfig
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/16 14:12
  • @Version 1.0**/
    @Configuration
    br/>**/
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().anyRequest().permitAll()
    .and().csrf().disable();
    }
    }

    
    - 然后向程序的ioc容器中注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
    ~~~java
    package com.eelve.lovin;

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;

/**

  • @ClassName LovinRibbonClientApplication
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 16:59
  • @Version 1.0**/
    @SpringBootApplication
    br/>**/
    @SpringBootApplication
    public class LovinRibbonClientApplication {

    public static void main(String[] args) {
    SpringApplication.run(LovinRibbonClientApplication.class,args);
    }

    @Bean@LoadBalanced
    br/>@LoadBalanced
    return new RestTemplate();
    }
    }

    
    - 然后编写一个**HelloService**
    ~~~java
    package com.eelve.lovin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**

  • @ClassName HelloService
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:02
  • @Version 1.0**/
    @Service
    br/>**/
    @Service
    br/>@Autowired

    public String getHello() {
    //这里的lovineurkaclient是我上一篇文章新建的eureka客户端的名称
    return restTemplate.getForObject("http://lovineurkaclient/hello",String.class);
    }
    }

    
    - 再编写一个**HelloController**
    ~~~java
    package com.eelve.lovin.controller;

import com.eelve.lovin.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

  • @ClassName HelloController
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/15 17:05
  • @Version 1.0**/
    @RestController
    br/>**/
    @RestController

    @Autowired
    HelloService helloService;

    @RequestMapping("hello")
    public String hello(){
    return helloService.getHello();
    }
    }

    
    # 叁、启动测试
    - 依次启动eureka的服务端和两个客户端,以及新建的lovin-ribbon-client
    ![我们可以看到服务已经全部启动成功](https://i.loli.net/2019/08/23/pa1X8eByNhltP4C.png)
    我们可以看到服务已经全部启动成功
    - 然后访问http://localhost:8805/hello
    ![我们可以看到已经可以通过ribbon调到我们建立的eureka客户端了](https://i.loli.net/2019/08/23/TXoNiuvYhGO9Hc3.png)
    我们可以看到已经可以通过ribbon调到我们建立的eureka客户端了
    - 再次请求接口观察返回
    ![我们可以看到我们调到了通过ribbon负载的另外一个接口](https://i.loli.net/2019/08/23/o9Cm2LgBXb4qjF5.png)
    我们可以看到我们调到了通过ribbon负载的另外一个接口了,到这里我们就已经弄好了一个简单的ribbon负载。

肆、网络架构

  • 我们可以看到我们调用的服务不再是像再上一篇文章中的直接访问对应的服务,而是通过Ribbon的负载均衡的去调用的,而且这里说明一点,Ribbon的默认机制是轮询。
    SpringCloud之Ribbon

  • 最后的最后是本博客的源码,欢迎关注这一套SpringCloud的实践

相关内容

热门资讯

萧美琴称年花6000元新台币保... 海峡导报综合报道 岛内朝野对8年1.25万亿元(新台币,下同)的“防务特别条例”仍无共识,台湾地区副...
巴基斯坦消息人士:美伊谈判似已... 巴基斯坦权威消息人士6日表示,美国和伊朗通过巴基斯坦进行的谈判似已显现希望,“低调的谈判有望转化为切...
雷暴大风、短时强降水、局地冰雹... 今天上午我省多地晴朗在线、升温持续,10点京广线及以东地区升至25-27℃,预计午后最高气温除了西部...
无障碍阅读“建设蓝图”出炉!多... 【大河财立方消息】 5月6日消息,中国残联、教育部、文化和旅游部、国家新闻出版署、共青团中央、全国妇...
蚂蚁集团等入股大晓机器人 【大河财立方消息】天眼查App显示,近日,大晓机器人关联公司上海大晓无限机器人有限公司发生工商变更,...
南航扭亏的AB面:物流压舱、子... 【大河财立方 记者 陈诗昂】4月30日,南方航空举行2026年一季度业绩发布会。一季报显示,南航营收...
“五一”超800场好戏燃动全城... “五一” 假期落幕,全国文旅市场持续火爆、亮点频现。山东临沂琅琊古城凭借沉浸式国风演艺、全时段互动体...
Siri升级延期,苹果或赔17... 来源:市场资讯 (来源:界面新闻) 据多家外媒报道,当地时间5月5日,美国苹果公司就一起集体诉讼达成...
时代电气获得发明专利授权:“列... 证券之星消息,根据天眼查APP数据显示时代电气(688187)新获得一项发明专利授权,专利名为“列车...
高市早苗在澳大利亚下跪献花,网... 据日本外务省网站消息,当地时间5月4日,日本首相高市早苗对澳大利亚进行访问时,前往位于堪培拉的澳大利...