1、POM配置

  和普通Spring Boot工程相比,仅仅添加了Eureka、Ribbon、Hystrix依赖和Spring Cloud依赖管理

<dependencies>
  <!--添加Eureka Server依赖-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <!-- 负载均衡ribbon -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <!-- 断路器Hystrix -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
  </dependency>
  <!-- 服务健康检查,必须添加,否则此服务不会启动hystrix.stream -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <!--Spring Cloud依赖版本管理-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

02、使能Eureka Client

@SpringBootApplication
@EnableEurekaClient  //使能Eureka客户端
@EnableCircuitBreaker  //使能断路器
public class RibbonApplication {
@Bean
@LoadBalanced  //负载均衡的RestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}

03、src/main/resources工程配置文件application.yml

server:
port: 3001 #默认启动端口
spring:
application:
name: ribbon-hello-service-consumer #应用名
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1000/eureka/ #服务注册中心地址

04、负载均衡消费

@Service
public class HelloConsumerService {
@Autowired
private RestTemplate restTemplate; // 负载均衡消费
@HystrixCommand(fallbackMethod = "fallback")
public String hello() {
return this.restTemplate.getForObject("http://hello-service-provider/hello/", String.class);
}
//短路器打开时,执行此函数
public String fallback() {
return "Error";
}
}

05、测试使用服务

@RestController
public class HelloController {
@Autowired
private HelloConsumerService helloService; @GetMapping("/hello")
public String hello() {
return this.helloService.hello();
}
}

003客户端负载均衡Ribbon & 短路器Hystrix的更多相关文章

  1. 客户端负载均衡Ribbon

    客户端负载均衡Ribbon 一.Ribbon是什么 二.Ribbon实现客户端负载均衡 三.Ribbon负载均衡策略 四.Rest请求模板类解读 4.1 RestTemplate的GET请求 第一种: ...

  2. 客户端负载均衡Ribbon之一:Spring Cloud Netflix负载均衡组件Ribbon介绍

    Netflix:['netfliːks] ribbon:英[ˈrɪbən]美[ˈrɪbən]n. 带; 绶带; (打印机的) 色带; 带状物;v. 把…撕成条带; 用缎带装饰; 形成带状;     L ...

  3. 【Dalston】【第二章】客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  4. 客户端负载均衡Ribbon之二:Loadbalance的源码

    Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 像nginx可以使用负载均衡分配流量,ribbon为客户端提供负载均衡,dubbo服务调用里的负载均衡 ...

  5. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  6. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

  7. 五、springcloud之客户端负载均衡Ribbon

    一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...

  8. spring cloud 之 客户端负载均衡 Ribbon

    一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...

  9. 0403-服务注册与发现-客户端负载均衡-Ribbon的基本使用

    一.概述 问题1.上一篇文章已说明如何注册微服务,但是调用方如何调用,以及如何防止硬编码.即电影微服务调用用户微服务 问题2.用户微服务多个节点,调用服务方如何负载均衡 二.实现负载均衡方式 2.1. ...

随机推荐

  1. WINDOWS SERVER 2012标准版密钥

    Windows Server 2012 R2 安装密钥(只适用安装,不支持激活) 标准版 = NB4WH-BBBYV-3MPPC-9RCMV-46XCB MMPXK-NBJDQ-JPM34-WX3FM ...

  2. appium+android各配置参数获取'platformName'、'platformVersion'、appActivity、deviceName、webdriver.Remote

    图中1的获取--'platformName'.'platformVersion' 点击appium右上角的运行按钮,可通过上面查看platformName 和 platformVersion 平台版本 ...

  3. Navicat 连接腾讯云

    1.dos窗口下进入mysql,进行远程登录授权 (1)进行授权 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1 ...

  4. Rstudio所有快捷键 “原版+中文” 整理

  5. JavaScript对象基础知识总结

    1.什么叫JavaScript对象? 定义:名值对的集合.简单的讲就是容纳属性值和属性值的容器,这些属性可以是无序的,基本上JavaScript中所有的事物都可以看成对象. 拓展:我们经常说,数组也是 ...

  6. 微信小程序回到顶部的两种方式

    一,使用view形式的回到顶部 <image src='../../img/button-top.png' class='goTop' hidden='{{!floorstatus}}' bin ...

  7. Dubbo---初识

    1.概述 1.1 Dubbo是一款高性能.轻量级的java RPC框架: 1.2 Dubbo提供的功能: 面向接口的远程调用: 智能容错.负载均衡: 服务注册.发现: 1.3 Dubbo架构 Prov ...

  8. Binder Native 层(二)

    Binder 框架及 Native 层 Binder机制使本地对象可以像操作当前对象一样调用远程对象,可以使不同的进程间互相通信.Binder 使用 Client/Server 架构,客户端通过服务端 ...

  9. JDBC(1)-连接数据库

    主要步骤包括: 加载驱动: 连接数据库: 使用语句操作数据库: 关闭数据库连接,释放资源. 1.需要导包: 2.加载数据驱动: mysql驱动名:com.mysql.jdbc.Driver 加载方式: ...

  10. PHP的file_get_contents()方法,将整个文件读入字符串中

    <?php $post_data = file_get_contents("e:\\1.txt"); echo $post_data; ?> 更多信息看这里:http: ...