feign中的hytrix和turbin配置

简介: feign中的hytrix和turbin配置这里我用了两个生产者和两个消费者进行演示,如下图(画的不好看,凑活看看):image.png这里我就只讲下怎么注册到dashbord和相关的配置,提供者和消费者等代码可以去下载查看:https://github.

feign中的hytrix和turbin配置

这里我用了两个生产者和两个消费者进行演示,如下图(画的不好看,凑活看看):

img_2991a6088801857ab49310cdb69a5a2e.png
image.png

这里我就只讲下怎么注册到dashbord和相关的配置,提供者和消费者等代码可以去下载查看:

https://github.com/fengcharly/springCloud-ribbon-turbine.git  

1.hystrix的配置:

这里我将熔断器(或者称为断路器配置到了消费者端):

启动类:

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>

    <groupId>com.consumer</groupId>
    <artifactId>stu-consumer-feign-hytrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>stu-consumer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>Brussels-SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


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

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

        <!--mybatis与mysql-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--druid依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.25</version>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>

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

        <!--session集群管理-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

        <!--zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>

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

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

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


        <!--配置huystrix-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

StuConsumerApplication:

package com.consumer.stuconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class StuConsumerApplication {

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

控制层:

import com.consumer.stuconsumer.entity.Student;
import com.consumer.stuconsumer.feign.UserFeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ConsumerController {
    @Resource
    private UserFeignClient userFeignClient;
    @RequestMapping("/getAll/{id}")
    public Student getAll(@PathVariable("id") Integer id) {
        Student stu = userFeignClient.getAll(id);
        return stu;
    }
}

Feign:(这个是配置断路器的主要配置)

ConsumerFeign:

import com.consumer.stuconsumer.entity.Student;
import com.consumer.stuconsumer.feign.fallbackfactory.UserFallbackFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//使用FeignClient 告知发布方的应用名称 默认使用ribbon进行负载均衡
@FeignClient(name = "stu-provide",fallbackFactory = UserFallbackFactory.class)
public interface ConsumerFeign {
    @RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET)
    public Student getAll(@PathVariable("id") Integer id);

}

UserFeignWithFactory:

import com.consumer.stuconsumer.feign.ConsumerFeign;
//这个是hystrix的类
public interface UserFeignWithFactory extends ConsumerFeign {

}

UserFallbackFactory:

import com.consumer.stuconsumer.entity.Student;
import com.consumer.stuconsumer.feign.ConsumerFeign;
import com.consumer.stuconsumer.feign.feignclientwithfactory.UserFeignWithFactory;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
//断路器设置
//这个是hystrix的类
@Component
public class UserFallbackFactory implements FallbackFactory<ConsumerFeign> {
    @Override
    public ConsumerFeign create(Throwable throwable) {
        return new UserFeignWithFactory(){
            @Override
            public Student getAll(Integer id) {
                return null;
            }
        };
    }
}

2.建立dashbord

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>

    <groupId>com</groupId>
    <artifactId>stu-dashbord</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>stu-dashbord</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Finchley.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-hystrix-dashboard</artifactId>
        </dependency>

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

        <dependency>

            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</artifactId>
        </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>


</project>

StuDashbordApplication:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableHystrixDashboard
public class StuDashbordApplication {

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

application.yml:

server:
  port: 8030

3.启动dashbord并访问:http://localhost:8030/hystrix:

img_d0dc9ec465e087c66cf8c0678eeb64c8.png
image.png

输入http://localhost:9301/hystrix.stream

img_447ff21c85d8dad4bdcff5d9c0cec2ba.png
image.png

然后 我们访问http://localhost:9301/getAll/1,可以在仪表盘中看到如下的信息:

img_be46cd5feac9a0f2bfc2bcaade9cfa31.png
image.png

这时,我们的dashbord单个应用监控完毕,但是我们在实际应用中往往不止用到一个应用,这时就需要我们来监控多个应用,这边我们可以配置turbine来进行应用的监控集群:

4.建立turbine

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>

    <groupId>com</groupId>
    <artifactId>stu-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>stu-turbine</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Finchley.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-turbine</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>
    </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>


</project>

StuTurbineApplication:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class StuTurbineApplication {

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

application.yml(重点关注):

server:
  port: 8031
spring:
  application:
    name: stu-hystrix-turbine
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
turbine:
  aggregator:
    clusterConfig: default
  appConfig:  stu-consumer-feign-hytrix,stu-consumer
  clusterNameExpression: "'default'"
  instanceUrlSuffix: /hystrix.stream
#turbine.instanceUrlSuffix=/xxx/hystrix.stream

我们这边一定要配置 instanceUrlSuffix: /hystrix.stream,当然可以少一个/,这边不配置这个路径的话会报路径错误,这个是指定他路径后缀用的.

然后我们启动turbine,这时候我们在仪表盘再进行监控就可以看到多个应用的监控信息了:

配置监控的路径:http://localhost:8031/turbine.stream

img_5cfb64859303cb97f3db5083b451cc51.png
image.png

仪表盘显示状态:

img_ea2183eb3aa5fc8164d0cfb7e2037a7a.png
image.png

附上源码地址:https://github.com/fengcharly/springCloud-ribbon-turbine.git

相关文章
|
数据采集 Ubuntu 数据挖掘
PyHubWeekly | 第七期:5款Python小工具,最后一款会给惊喜
我们日常工作和学习中,会经常做一些重复性的动作,这让人很痛苦,却又很无奈,本期介绍的第5款工具就可以彻底解放你在网页端的各种重复且枯燥的工作。下面,就来看一下本期的内容吧。
PyHubWeekly | 第七期:5款Python小工具,最后一款会给惊喜
|
弹性计算 网络协议 Linux
阿里云搭建我的世界服务器
以阿里云ECS服务器作为frp内网穿透中转,搭建我的世界服务器。
5133 0
|
3月前
|
数据采集 监控 API
淘宝店铺所有商品 API 怎么拿?一文讲透官方接口 + 第三方接口
本文详解淘宝商品数据合规获取方案:官方TOP接口(需店铺授权,仅限自身店铺)与第三方接口(支持竞品监控)。强调分页拉取、字段精简、频率控制三大要点,避开爬虫封禁风险,附完整接入步骤与避坑指南。
509 0
|
6月前
|
人工智能 运维 监控
AI 驱动网络攻击自主化演进与传统防御体系适配性研究
本文基于Anthropic 2025–2026年832个恶意账号实测数据,揭示AI正驱动网络攻击从人工主导迈向全链路自主化:67%攻击者用AI筹备攻击,中高风险者占比由33%升至56%,AI已深度渗透后渗透阶段。研究指出MITRE ATT&CK等传统框架失效、防御体系滞后,并提出覆盖行为监测、框架迭代、权限管控、人员赋能的分层防御方案。(239字)
568 4
|
7月前
|
存储
办公Agent的“询问-澄清”机制:如何处理模糊需求(如“整理上周客户邮件”)
本文揭秘办公Agent如何应对模糊指令(如“整理客户邮件”),提出三层“询问-澄清”机制:①用常识默认值自动填充;②仅聚焦最多3个关键不确定点精准提问;③支持边执行边确认。附真实状态机代码与避坑指南,让Agent像资深助理一样懂分寸、少打扰、真靠谱。(239字)
817 3
|
9月前
|
存储 缓存 API
RAG、MCP与智能体:大模型落地的三道坎
本文探讨大模型落地的三大关键技术:RAG(让模型动态检索知识)、MCP(标准化模型调用工具)、智能体(具备规划、记忆与执行能力的多步任务系统)。三者协同,推动AI从单次问答迈向稳定可靠的产品级应用。(239字)
558 4
|
10月前
|
SQL 关系型数据库 数据挖掘
PostgreSQL窗口函数从入门到实操
PostgreSQL窗口函数是数据分析利器:统计不丢行、细节全保留!本文用大白话讲透核心概念(分区/排序/窗口帧),配可运行示例,带你零基础掌握排名、移动平均、累积求和等高频场景,学完即用。
|
前端开发 JavaScript Android开发
Jetpack MVVM 七宗罪之四: 使用 LiveData/StateFlow 发送 Events
Jetpack MVVM 七宗罪之四: 使用 LiveData/StateFlow 发送 Events
799 0
|
JavaScript Java PHP
NSSCTF之Web篇刷题记录(12)
NSSCTF之Web篇刷题记录(12)
1549 123