SpringCloud之Ribbon

前面的话】书接上文,本文的某些知识依赖我的上一篇文章: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依赖
<parent>
        <artifactId>lovincloud</artifactId>
        <groupId>com.eelve.lovincloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lovin-ribbon-client</artifactId>
    <packaging>jar</packaging>
    <name>ribbonclient</name>
    <version>0.0.1</version>
    <description>ribbon的client</description>

    <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-security</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 这里为了安全,我这里还是添加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](https://my.oschina.net/u/570656):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](https://my.oschina.net/u/3112573) SecurityConfig
 * @Description TDO
 * [@Author](https://my.oschina.net/arthor) zhao.zhilue
 * [@Date](https://my.oschina.net/u/2504391) 2019/8/16 14:12
 * @Version 1.0
 **/
@Configuration
@Order(0)
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开启负载均衡的功能。
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
@EnableDiscoveryClient
public class LovinRibbonClientApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
  • 然后编写一个HelloService
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
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    public String getHello() {
        //这里的**lovineurkaclient**是我上一篇文章新建的eureka客户端的名称
        return restTemplate.getForObject("http://lovineurkaclient/hello",String.class);
    }
}
  • 再编写一个HelloController
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
public class HelloController {

    @Autowired
    HelloService helloService;

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

叁、启动测试

  • 依次启动eureka的服务端和两个客户端,以及新建的lovin-ribbon-client 我们可以看到服务已经全部启动成功 我们可以看到服务已经全部启动成功
  • 然后访问http://localhost:8805/hello 我们可以看到已经可以通过ribbon调到我们建立的eureka客户端了 我们可以看到已经可以通过ribbon调到我们建立的eureka客户端了
  • 再次请求接口观察返回 我们可以看到我们调到了通过ribbon负载的另外一个接口 我们可以看到我们调到了通过ribbon负载的另外一个接口了,到这里我们就已经弄好了一个简单的ribbon负载。

肆、网络架构

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

转载于:https://my.oschina.net/u/3082380/blog/3099151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个基于Spring Boot的开发工具集,提供了一系列用于构建分布式系统的解决方案。其中,Feign和Ribbon是Spring Cloud中常用的两个组件。 Feign是一个声明式的Web服务客户端,用于简化使用Spring Cloud的服务之间进行通信的过程。它通过支持注解方式来定义和使用服务接口,底层使用的是基于反射的动态代理技术,将接口和实际调用的服务进行映射。Feign通过集成Ribbon来实现负载均衡的功能。 Ribbon是一个负载均衡的组件,它根据一系列的负载均衡策略,从多个服务实例中选择一个要调用的实例。Ribbon通过监听Eureka注册中心上已注册的服务列表,并通过默认的轮询算法选择一个实例。在Feign中,Ribbon用于根据服务接口定义的URL和方法,选择一个具体的服务进行调用。 Feign和Ribbon的原理可以简单总结如下:首先,Feign通过使用@EnableFeignClients注解开启Feign功能,扫描包中带有@FeignClient注解的接口定义。接着,Feign将这些接口定义转化为动态代理对象,在调用接口方法时,实际上是通过动态代理对象进行了解析和转发,最终会调用到具体的服务实例上。此时,Ribbon会根据一定的策略从多个服务实例中选择一个实例进行调用,并返回调用结果。整个调用过程是通过HTTP协议进行通信的。 总之,Spring Cloud中的Feign和Ribbon组件能够实现微服务之间的通信和负载均衡功能。Feign简化了基于HTTP的服务接口定义和调用的过程,而Ribbon则负责根据一定的负载均衡策略选择合适的服务实例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值