Spring cloud系列七 为@Feign中集成的Ribbon进行个性化配置

1. 概述

上文Spring cloud系列六 Ribbon的功能概述、主要组件和属性文件配置已经介绍了ribbon的主要组件和用法。我们已知Spring Cloud的@Feign已经集成了ribbon的功能。本文我们介绍如何为集成在@Feign中的ribbon进行个性化配置。

主要内容如下:

  • 为集成在@Feign中的ribbon进行个性化配置
  • 通过@RibbonClients和@RibbonClient配置ribbon
  • 通过属性文件配置ribbon

2. 工程说明

本文涉及到以下几个工程:

  • cloud-registration-center:注册中心,关于这个工程,详细见之前的本系列文章
  • cloud-service-ribbon:模拟服务端,注册到注册中心并对外提供调用接口
  • cloud-consumer-ribbon:模拟客户端,配置个性化ribbon,通过@Fegin(含ribbon)调用服务接口

3. cloud-service-ribbon

模拟服务端,注册到注册中心并对外提供调用接口。这个服务非常简单,对外提供2个http rest接口供客户端调用

3.1. pom.xml

 <parent>
        <artifactId>cloudgparent</artifactId>
        <groupId>com.hry.spring.cloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../cloud-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-service-ribbon</artifactId>

3.2. bootstrap-ribbon.yml

配置注册中心的地址和自己的服务名称cloud-ribbon-service

# port
server:
  port: 11083

spring:
  application:
    # 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符
    name: cloud-ribbon-service

eureka:
  client:
    serviceUrl:
      # 服务器注册/获取服务器的zone
      defaultZone: http://127.0.0.1:10761/eureka/
      # defaultZone: http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/eureka/
  instance:
    prefer-ip-address: true

3.3. SimpleCtl.java:

提供简单的服务接口

  1. ribbonClientCall:立即返回一个随机字符串
  2. ribbonClientCallSleep:休息5s,然后返回一个随机字符串。用于测试ribbon的超时调用
@RestController
public class SimpleCtl {
    private AtomicInteger count = new AtomicInteger();
    private AtomicInteger sleepCount = new AtomicInteger();

    @RequestMapping(value="/ribbon/simple")
    public String ribbonClientCall(){
        int newCount = count.incrementAndGet();
        return "ribbon" + newCount + ": " + ThreadLocalRandom.current().nextInt(1000);
    }

    @RequestMapping(value="/ribbon/sleep")
    public String ribbonClientCallSleep(){
     
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值