这一章节讲如何使用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. 10大Python开源项目推荐(Github平均star2135)

    翻译 | suisui 来源 | 人工智能头条(AI_Thinker) 继续假日充电系列~本文是 Mybridge 挑选的 10 个 Python 开源项目,Github 平均star 2135,希望 ...

  2. JS基础学习四:绑定事件

    添加事件 IE: attachEvent Other: addEventListener var button = document.getElementById("buttonId&quo ...

  3. 在SQLSERVER中创建聚集索引

    CREATE CLUSTERED INDEX CLUSTER_id ON TABLE_name(ID)------批量

  4. kuangbin专题十二 HDU1087 Super Jumping! Jumping! Jumping! (LIS)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. HDU - 6416 :Rikka with Seam(DP & 前缀和 & 数学)

    pro:给定N*M的矩阵,现在让你在每一行删去一个位置,然后形成新N*(M-1)的矩阵,问有多少种不同的新的矩阵.需要满足相邻行删去的位置不大于K. (题目是01矩阵,其实任意矩阵都可以做,本题算法里 ...

  6. [Groovy]Parse properties file in Groovy

    def props = new Properties() new File("foo.properties").withInputStream { s -> props.lo ...

  7. C语言中结构、联合、枚举的说明

    复杂的数据类型 一般的步骤: 1.声明模板 2.定义变量,分配内存空间 3.初始化 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  8. Java中利用JFrame创建窗体

    1. 一个简单例子: public class Test(){ public static void main(String[] args){ JFrame frame = new JFrame(); ...

  9. No bean named 'xxxxxxx' available--springboot 上线打war包

    springboot项目发布上线后,报错:No bean named 'xxxxxxx' available 因为我开发时pom用的jar,但上线发布war.解决方法: 1.pom.xml <p ...

  10. APP测试总结1

    1.安装.卸载测试 安装.卸载测试主要针对编译后源程序生成的APK安装文件 主要测试点: 1).生成的APK文件在真机上可以安装及下载 2).Android手机端的通用安装工具,如:豌豆荚及91助手等 ...