zoukankan      html  css  js  c++  java
  • springCloud 的 hystrix(熔断器) 的简单入门

    基于  eureka 服务于发现 (集群模式) 6001   6002  eureka 注册中心 的 熔断器 hystix简单代码入门

    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+ "无效--@HystrixCommand","无效的数据库");
    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 有效 pid
    http://localhost:8001/product/get/-1 无效 pid

    无效 地址  -1

    有效 地址  1

     

  • 相关阅读:
    类模板和函数模板
    vector用法
    the swap trick用于锐减过剩容量
    SIGHUP信号
    linux页表机制
    linux中sigsuspend和pause的区别
    最长回文串:LeetCode:Longest Palindromic Substring
    Implement strStr()
    ffmpeg知多少~~~
    下面可能会更新很多。。。
  • 原文地址:https://www.cnblogs.com/wangshichang/p/11435992.html
Copyright © 2011-2022 走看看