springCloud 的 hystrix(熔断器) 的简单入门

1..... pom.xml 依赖 jar

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>spring-cloud-parent</artifactId>
 7         <groupId>com.wsc</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9         <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12
13     <artifactId>hystrix-8001</artifactId>
14
15
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.8</maven.compiler.source>
19         <maven.compiler.target>1.8</maven.compiler.target>
20     </properties>
21     <dependencies>
22         <dependency>
23             <groupId>com.wsc</groupId>
24             <artifactId>common</artifactId>
25             <version>1.0-SNAPSHOT</version>
26         </dependency>
27         <!--添加hystirx依赖-->
28         <dependency>
29             <groupId>org.springframework.cloud</groupId>
30             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
31         </dependency>
32         <!--服务提供者注册进服务中心-->
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
36         </dependency>
37         <!--springboot web启动器-->
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-web</artifactId>
41         </dependency>
42
43         <!--导入 mybatis 启动器-->
44         <dependency>
45             <groupId>org.mybatis.spring.boot</groupId>
46             <artifactId>mybatis-spring-boot-starter</artifactId>
47         </dependency>
48         <dependency>
49             <groupId>org.springframework.boot</groupId>
50             <artifactId>spring-boot-starter-test</artifactId>
51         </dependency>
52         <dependency>
53             <groupId>junit</groupId>
54             <artifactId>junit</artifactId>
55         </dependency>
56         <dependency>
57             <groupId>mysql</groupId>
58             <artifactId>mysql-connector-java</artifactId>
59             <version>8.0.13</version>
60         </dependency>
61         <dependency>
62             <groupId>com.alibaba</groupId>
63             <artifactId>druid</artifactId>
64         </dependency>
65     </dependencies>
66 </project>

pom.xml

2..... hystrix-8001 包

resources / mybatis / mapper / ProductMapper.xml  配置 映射文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4 <mapper namespace="com.wsc.core.mapper.ProductMapper">
 5     <select id="findAll" resultType="Product">
 6         select * from product;
 7     </select>
 8
 9     <select id="findById" resultType="Product" parameterType="Long">
10         select * from product where pid=#{pid};
11     </select>
12
13     <insert id="add"    parameterType="Product" >
14         insert into product values (null,#{product_name},#{db_source});
15     </insert>
16 </mapper>

ProductMapper.xml

resources / mybatis  / mybatis.cfg.xml  配置 开启驼峰命名

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <settings>
 7         <!--开启驼峰命名-->
 8         <setting name="mapUnderscoreToCamelCase" value="true"/>
 9     </settings>
10 </configuration>

mybatis.cfg.xml

resources / application.yml   数据库 扫描文件 注册客户端

server:
  port: 8001
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径
  type-aliases-package: com.wsc.core.pojo # entity别名类所在包
  mapper-locations: mybatis/mapper/*.xml    # mapper映射文件
spring:
  application:
    name: microserver-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8
    password: wsc
    username: root
    dbcp2:
      min-idle: 5                         # 数据库连接池的最小维持连接数
      initial-size: 5                     # 初始化连接数
      max-total: 5                        # 最大连接数
      max-wait-millis: 150                                    # 等待连接获取的最大超时时间

eureka:
  client:
    register-with-eureka: true             #服务注册开关
    fetch-registry: true                  #服务发现开关
    service-url:
      defaultZone: http://eureka6001.com:6001/eureka/, http://eureka6002.com:6002/eureka/    # http://localhost:6001/eureka # 1 显示主机名
      instance:
        instanceId: ${spring.application.name}:${server.port}-hystrix   #  2   指定实例ID 不显示主机名
        preferipAddress: true

service / ProductService

 1 package com.wsc.core.service;
 2
 3 import com.wsc.core.pojo.Product;
 4
 5 import java.util.List;
 6
 7 public interface ProductService {
 8     public List<Product> findAll();
 9
10     public Product findById(Long id);
11
12     public Boolean add(Product product);
13 }

ProductService

service /impl / ProductServiceImpl

 1 package com.wsc.core.service.impl;
 2
 3 import com.wsc.core.mapper.ProductMapper;
 4 import com.wsc.core.pojo.Product;
 5 import com.wsc.core.service.ProductService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8
 9 import java.util.List;
10
11 /**
12  * @version 1.0
13  * @ClassName ProductServiceImpl
14  * @Description TODO
15  * @Author WSC
16  * @Date 2019/8/27 10:53
17  **/
18 @Service
19 public class ProductServiceImpl implements ProductService {
20     @Autowired
21     private ProductMapper productMapper;
22     @Override
23     public List<Product> findAll() {
24         return productMapper.findAll();
25     }
26
27     @Override
28     public Product findById(Long id) {
29         return productMapper.findById(id);
30     }
31
32     @Override
33     public Boolean add(Product product) {
34         return productMapper.add(product);
35     }
36 }

ProductServiceImpl

mapper / ProductMapper

 1 package com.wsc.core.mapper;
 2
 3 import com.wsc.core.pojo.Product;
 4
 5 import java.util.List;
 6
 7 public interface ProductMapper {
 8     public List<Product> findAll();
 9
10     public Product findById(Long id);
11
12     public Boolean add(Product product);
13 }

ProductMapper

controller / ProductController

 1 package com.wsc.core.controller;
 2
 3 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
 4 import com.wsc.core.pojo.Product;
 5 import com.wsc.core.service.ProductService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.web.bind.annotation.*;
 8
 9 import java.util.List;
10
11 /**
12  * @version 1.0
13  * @ClassName ProductController
14  * @Description TODO
15  * @Author WSC
16  * @Date 2019/8/27 11:06
17  **/
18 @RestController //返回json数据
19 public class ProductController {
20     @Autowired
21     private ProductService productService;
22     //查询全部
23     @RequestMapping(value ="/product/get/list",method = RequestMethod.GET)
24     public List<Product> getAll(){
25         return productService.findAll();
26     }
27
28 //查询id
29 //get 方法出现异常  则会调用  hystrixGet方法进行处理
30     @HystrixCommand(fallbackMethod = "getFallBack")
31     @RequestMapping(value = "/product/get/{pid}",method = RequestMethod.GET)
32     public Product getById(@PathVariable("pid")Long pid){
33         Product productServiceById = productService.findById(pid);
34         // 模拟异常
35         if(productServiceById==null){
36             throw new RuntimeException("pid"+pid+"无效");
37         }
38         return productServiceById;
39     }
40     // 当get方法 出现异常的时候 会调用此方法  注意方法的返回值 参数列表  要与原方法一致
41     public Product getFallBack(@PathVariable("pid")Long pid){
42         return new Product(pid,"pid" +pid+ "无效[email protected]","无效的数据库");
43
44     }
45 //添加
46     @RequestMapping(value ="/product/get/add",method = RequestMethod.POST)
47     public boolean add(@RequestBody Product product){
48         return productService.add(product);
49     }
50 }

Start_8001_hystrix

 1 package com.wsc.core;
 2
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 8
 9 /**
10  * @version 1.0
11  * @ClassName Start
12  * @Description TODO
13  * @Author WSC
14  * @Date 2019/8/27 10:54
15  **/
16 @EnableHystrix  //开启Hystrix  熔断机制的支持
17 @EnableEurekaClient //  本服务启动后 自动注册进Eureka中心
18 @MapperScan("com.wsc.core.mapper") //mapper扫描包 类 ProductMapper
19 @SpringBootApplication
20 public class Start_8001_hystrix {
21     public static void main(String[] args) {
22         SpringApplication.run(Start_8001_hystrix.class,args);
23     }
24 }

Start_8001_hystrix

测试地址输入:

http://eureka6002.com:6002/  6002注册中心
http://eureka6001.com:6001/  6001注册中心http://localhost:8001/product/get/1 有效 pidhttp://localhost:8001/product/get/-1 无效 pid

无效 地址  -1

有效 地址  1

原文地址:https://www.cnblogs.com/wangshichang/p/11435992.html

时间: 2024-08-30 14:47:50

springCloud 的 hystrix(熔断器) 的简单入门的相关文章

springcloud+eureka简单入门案例

springcloud+eureka简单入门案例 一.服务提供者 直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突 配置文件(src/main/resources/application.yml) server: port: 8000 二.服务消费者 服务消费者的依赖在这个单独的demo中其实可有可无,亲测不添加,也可以实现demo服务提供能 三.服务消费者启动类里注入RestTemplate,用于调用远程服务 import org.springframew

SpringCloud实战-Hystrix请求熔断与服务降级

我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险.Spring Cloud Netflix Hystrix就是隔离措施的一种实现,可以设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,从而提高分布式系统的稳定性. 生活中举个例子,如电力过载保护器,当电流过大的的时候,出问题,过载器会自动断开,从而保护电器不受烧坏.因此Hystrix请求熔

正则表达式简单入门

 正则表达式简单入门    正则表达式在平常编程中有着大量的应用,对于任何一个想学习编程的人来说,正则表达式是一个必须掌握的知识. 废话不多说,下面先对正则表达式做一个简单的入门介绍,在后续的文章中,将会进行详细的介绍.    一.元字符 元字符一共有12个:$ ( ) [ { ? + * . ^ \ | 元字符有特殊的含义,如果要使用其字面值,则必须对其进行转义. 如: \$  \*  \( 等等 二.控制字符或不可打印字符 \a  警报 \e  退出 \f  换页 \n  换行 \r 

程序员,一起玩转GitHub版本控制,超简单入门教程 干货2

本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注头条号.每日更新.也可以添加小编微信:fullstackCourse.一起交流,获取最新全栈教程信息.因为FQ原因,不能下载客户端的同仁,可以关注后回复“GitHub客户端”获取安装软件. 上篇教程:GitHub这么火,程序员你不学学吗? 超简单入门教程 干货 GitHub概念部分出现了一丝纰漏.为

iBatis简单入门教程

iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了. 官网为:http://www.mybatis.org/ 搭建iBatis 开发环境: 1 .导入相关的jar 包,ibatis-2.3.0.677.jar .mysql-connector-java-5.1.6

Asp.Net MVC学习总结(一)——Asp.Net MVC简单入门

出处:http://www.cnblogs.com/SeeYouBug/p/6401737.html 一.MVC简单入门 1.1.MVC概念 视图(View) 代表用户交互界面,对于Web应用来说,可以概括为HTML界面,但有可能为XHTML.XML和Applet. 模型(Model) 表示用户对其数据的操作的一个封转.可以分为视图模型(view model)和领域模型(domain models),视图模型就是在视图与控制器之间传输数据的一个封转,而领域模型就是业务逻辑,后台数据模型等的一个集

Java日志系统---Logger之简单入门

Java 中自带的日志系统,今天抽空了解了一点,算是入了门,所以将自己的一些心得记录下来,以备日后查看,有兴趣的朋友,看到此文章,觉得有错误或需要添加的地方,请在下方评论留言,大家可以共同进步,谢谢:) Java中关于日志系统的API,在 java.util.logging 包中,在这个包中,Logger类很重要. Logger类是用来记录 某个级别的日志消息: 级别共分为以下几类,从上倒下,级别依次下降: SEVERE(严重)------级别最高 WARNING(警告) INFO CONFIG

响应式网页设计简单入门(强烈推薦!!!!)

响应式网页设计简单入门 Overview: 构造基本的HTML页面 动态加载样式表 Viewport 字体缩放 侧边栏 导航菜单 图片自适应 其他 总结 说到响应式网页设计(Responsive web design),最近在谷歌加上碰到个奇葩贴子,通过一个原始到无法再简单的网页Motherfucking Website及满屏幕的fuck道出了网页设计的真谛,这孩子不是个激进分子就是个报复社会型的货没错,虽然整篇文章就像是泼妇骂街,但我特么是笑着读完的.. 统计了下全文共用Fuck (包括fuc

Android HttpGet() 请求简单入门实例

HttpClient httpclient = new DefaultHttpClient(); String url = "http://example.com"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add( new BasicNameValuePair( "param", "value" ) ); URI uri =

delphi指针简单入门

delphi指针简单入门:         看一个指针用法的例子:     1         var     2             X,   Y:   Integer;       //   X   and   Y   整数类型     3             P:   ^Integer;           //   P   指向整数类型的指针     4         begin     5             X   :=17; //   给   X   赋值     6