这一章节讲如何使用ribbon和hystrix.

在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息,

这里Artifact填写ribbonconsumer, 再次next,

这里选择如下图:

最后在Module Name中填写ribbon-consumer.

生成的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xum</groupId>
<artifactId>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ribbon-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

在src->main->resources下建立application.yml文件, 内容如下

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8771
spring:
application:
name: ribbon-consumer
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'

在RibbonConsumerApplication.java里的内容如下, 要加上@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient这三个注解.

package com.xum.ribbonconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @EnableCircuitBreaker
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication { @Bean
@LoadBalanced  // 这里是加载平衡, 我这里使用RestTemplate
RestTemplate restTemplate () {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}

然后在RibbonConsumerApplication.java文件夹同级目录下建立Controller文件夹, 里面写一个RibbonConsumerController.java, 内容如下:

package com.xum.ribbonconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
@RequestMapping(value = "/ribbonconsumer")
public class RibbonConsumerController {
private static final Logger LOG = LoggerFactory.getLogger(RibbonConsumerController.class); @Autowired
RestTemplate restTemplate; @RequestMapping(value = "/test")
@HystrixCommand(fallbackMethod = "testConsumer_ribbon") // 这里是HystrixCommand注解, 如果eureka-client项目没有启动, 那么就会转到本项目的fallbackMethod方法,即testConsumer_ribbon(), 就是下面的方法
public String testConsumer() { // 这里是通过getForEntity属性来调用eureka-client里的testone/test这条api
LOG.info("RibbonConsumerController=>testConsumer");
return restTemplate.getForEntity("http://eureka-client/testone/test",String.class).getBody();
}
  // 这里就是熔断器(Hystrix)自我保护的方法,其实是自定义的方法.
public String testConsumer_ribbon() {
LOG.info("RibbonConsumerController=>testConsumer_ribbon");
return "testConsumer_ribbon";
}
}

下面是eureka-client项目下testone/test的api返回内容, 这里是简写了,具体的内容可以回看前面eureka-client项目搭建.

    @RequestMapping(value = "/test")
public String test(@RequestParam(value = "name", required = false, defaultValue = "testOneClient")String name) {
String info = "Hi " + name + ", this is EurekaClient, port is " + port;
return info;
}

最后顺序启动如下项目:

1. eureka-server

2. config-server

3. eureka-client

4. ribbon-consumer

最后用post man发送api: http://localhost:8771/ribbonconsumer/test

或则浏览器打开这条api.

可以看到返回的内容就好像调用eureka-client下的api: http://localhost:8762/testone/test

如果eureka-client项目里没有启动, 那么Hystrix会实现自我保护, 会调用到(fallbackMethod = "testConsumer_ribbon")方法, 自定义的方法.

测试一下, 把eureka-client项目停止,

在post man或则浏览器中输入http://localhost:8771/ribbonconsumer/test, 内容如下:

可以看到, 会调用到testConsumer_ribbon()的保护方法里去.

SpringCloud的学习记录(5)的更多相关文章

  1. SpringCloud的学习记录(1)

    最近一段时间重新学习一边SpringCloud(有半年不用了),这里简单记录一下. 我用的是IntelliJ IDEA开发工具, SpringBoot的版本是2.1.3.RELEASE. 1. 构建M ...

  2. SPRINGCLOUD 开发学习记录

    一个简单的微服务系统:服务注册和发现,服务消费,负载均衡,断路器,智能路由,配置管理 服务注册中心: eureka是一个高可用组件,没有后端缓存,每一个实例注册后向注册中心发送心跳,默认情况下,eru ...

  3. SpringCloud的学习记录(8)

    这一章节讲zipkin-server. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等 ...

  4. SpringCloud的学习记录(7)

    这一章节讲zuul的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这里 ...

  5. SpringCloud的学习记录(6)

    这一章节讲fegin的使用. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息, 这 ...

  6. SpringCloud的学习记录(3)

    这一章节讲搭建config-server的项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...

  7. SpringCloud的学习记录(2)

    这一章节主要讲如何搭建eureka-client项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和A ...

  8. SpringCloud的学习记录(4)

    本篇基于上一篇写的, 在git上更改配置后, eureka-client如何更新. 我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp; 这就是说我们需要装rabb ...

  9. SpringCloud基础教程学习记录

    这个学习记录是学习自翟永超前辈的SpringCloud的基础教程. 自己写这个教程的目的主要是在于,想要更凝练总结一些其中的一些实用点,顺便做个汇总,这样自己在复习查看的时候更加方便,也能顺着自己的思 ...

随机推荐

  1. Luogu1829 JZPTAB

    JZPTAB 求\(\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)\) \(=\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{\gcd(i,j)}\) 枚举gcd ...

  2. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  3. Windows服务注意!

    安装不成功 报错,或者安装成功 不运行 ,要把引用的dll文件 放到obj \debug文件夹下

  4. PAT天梯赛 L1-049 天梯赛座位分配

    题目链接:点击打开链接 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] ...

  5. Linux系统centos中sudo命令不能用----提升权限

    gyx is not in the sudoers file.  This incident will be reported. 1.切换到root用户 su ,如果想要切换回去 exit 2.添加s ...

  6. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  7. Web项目和Windows应用程序的配置文件

    1.Web项目,配置文件应创建在Web项目下,即使是要把配置文件作为一个单独的文件进行配置(比如log4net.config),也需要把该配置文件放在Web项目下:同理Windows应用程序的化,配置 ...

  8. 表格排序插件datatables

    之前用过表格排序插件tinytables,用到后面,随着需求的更改,发现这个插件真的low到爆了,不适合用于多表格,只有一个表格的页面可以凑合着用,有很多局限性. 之后发现了一款表格排序插件datat ...

  9. storm定时器

    package com.example.mail; import org.apache.storm.Config; import org.apache.storm.LocalCluster; impo ...

  10. vue(4)hello world

    在前一章基础上开发. 1.下载vue.js.(https://cn.vuejs.org/v2/guide/installation.html) 在hello-vue根目录下创建js文件夹,并将该vue ...