概述

在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的。Spring cloud 有两种服务调用方式,一种是 ribbon + restTemplate,另一种是 feign。在这一篇文章首先讲解下基于 ribbon + rest。

Ribbon简介

Ribbon 是一个负载均衡客户端,可以很好的控制 http 和 tcp 的一些行为。

准备工作

  • 启动服务提供者

  • 启动Eureka注册中心

创建服务消费者(POM)

        <!--spring cloud starter ribbon-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-ribbon</artifactId>
       </dependency>
       
<!--spring cloud starter eureka-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

Application

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;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaRibbonClientApplication {    //使用注解@LoadBalanced标记RestTemplate
   @Bean
   @LoadBalanced
   public RestTemplate restTemplate() {
       return new RestTemplate();
  }

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

application.yml

server:
port: 9002

spring:
application:
  name: eureka-client-ribbon

eureka:
client:
  serviceUrl:
    defaultZone: http://localhost:1111/eureka/
instance:
  lease-renewal-interval-in-seconds: 10 #服务续约
  lease-expiration-duration-in-seconds: 15 #服务剔除>服务续约时间

创建测试的Controller

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

@RestController
@RequestMapping("/api")
@Slf4j
public class RibbonController {

   @Autowired
   private RestTemplate restTemplate;
   
   //loadbalanced客户端
   @Autowired
   private LoadBalancerClient loadBalancerClient;


   @GetMapping("/ribbon/hello")
   public String hello() {
       String result = restTemplate.getForObject("http://eureka-provider/hello",String.class);
       return result;
  }
}

在IDEA中配置一个工厂启动多个实例

步骤一

点击 Run -> Edit Configurations...

#步骤二

选择需要启动多实例的项目并去掉 Single instance only 前面的勾

#步骤三

通过修改 application.yml 配置文件的 server.port 的端口,启动多个实例,需要多个端口,分别进行启动即可。

创建服务消费者(Ribbon)的更多相关文章

  1. springcloud-Netflix创建服务消费者

    目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...

  2. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  3. SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  4. 7、服务发现&服务消费者Ribbon

    公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...

  5. Spring Cloud (3) 服务消费者-Ribbon

    在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...

  6. 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  7. springCloud学习-服务消费者(rest+ribbon)

    1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...

  8. SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  9. SpringCloud-创建服务消费者-Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

随机推荐

  1. 编程算法 - 背包问题(三种动态规划) 代码(C)

    背包问题(三种动态规划) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目參考: http://blog.csdn.net/caroline_wen ...

  2. ant的condition任务

    1.istrue isfalse:断言 真 假 <project name="testCondition"> <target name="test&qu ...

  3. WPF: Creation of Text Labels for 3D Scene

    原文:WPF: Creation of Text Labels for 3D Scene 转载:http://www.codeproject.com/KB/WPF/WPF_Text3D.aspx Do ...

  4. python实现身份证校验位计算

    身份证校验码的计算方法 1.将前面的身份证号码17位数分别乘以不同的系数.第i位对应的数为[2^(18-i)]mod11.从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 ...

  5. Hibernate4与hibernate3有错误的版本号的主要区别所造成的不一致

    Hibernate版本号修改 Hibernate4的修改较大仅仅有spring3.1以上版本号可以支持,Spring3.1取消了HibernateTemplate,由于Hibernate4的事务管理已 ...

  6. uva 1436 - Counting heaps(算)

    题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,如今为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一仅仅 ...

  7. WPF 自定义图片剪切器 - 头像剪切(扩展与完善、实时截图)

    原文:WPF 自定义图片剪切器 - 头像剪切(扩展与完善.实时截图) 一.说明:上一次写的"WPF 自定义图片剪切器 - 头像剪切.你懂得"存在明显的缺陷,由于篇幅较长.重新写了一 ...

  8. UNITY VR 视频/图片 开发心得(二)

    上回说到了普通的全景图片,这回讲真正的VR. 由于这种图片分为两部分,所以我们需要两个Camera对象以及两个球体.首先新建一个Camera对象,并将其命名为RightEye(其它名字也无妨,只要你自 ...

  9. iOS UILabel显示html标签

    iOS7以后系统提供了显示html标签的方法 UIKIT_EXTERN NSString *const NSHTMLTextDocumentType NS_AVAILABLE_IOS(7_0); 直接 ...

  10. 浅谈Android高通(Qualcomm)和联发科(MTK)平台

    一款CPU好不好是要从多个方面考虑的,并不是说简简单单看一个主频.几个核心数就完了,更重要的是它的综合实力到底有多强,这里面当然也会牵扯到价格问题,性能相似当然是便宜的获胜,这是毋庸置疑的. 事实上, ...