springcloud hystrix 实现的简单例子

hystrix:超时熔断,个人感觉理解就像是trycatch一样,当出现异常,需要怎么处理的一种机制。

1.新建项目:ms-hystrix-consumer

1.pom文件:

    <!-- 注册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>
        <!--  web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- feigin的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
       <!-- hystrix 的依赖  -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--hystrix command -->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.12</version>
        </dependency>

2.resource :application.yml文件

#设置默认的服务超时时间为5s
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000
server:
  port: 8005
spring:
  application:
    name: ms-hystrix-consumer
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://ljf:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    metadata-map:
       business: Provide ms-fegin-client
       project-team: java-team
#将feging中的超时熔断机制给关掉
feign:
  hystrix:
    enabled: false
#设置默认的服务超时时间为5s
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

3.config:

package config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixUserConfig {
    /**
     * //feign配置日志
     * @return
     */
    @Bean
    public String showSmile() {
        String str="笑只是一种表情,与快乐无关,我是hystrix!!!";
        System.out.println(str);
        return str;
    }
}

4.service:@FeignClient(name="ms-eureka-provider",configuration = HystrixUserConfig.class)

package com.ljf.weifuwu.springcloud.hystrix.service;

import com.ljf.weifuwu.springcloud.hystrix.model.HystrixUser;
import config.HystrixUserConfig;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name="ms-eureka-provider",configuration = HystrixUserConfig.class)
public interface HystrixUserFegin {
    //和ms-eureka-provider模块中的路径要保持一致/eureka-provider/{id}
    //  @GetMapping("/eureka-provider/{id}")
    //    public EurekaUser getSingleUser(@PathVariable Long id)
    @RequestMapping(value = "/eureka-provider/{id}", method = RequestMethod.GET)
    public HystrixUser getSingleUser(@PathVariable("id") Long id);
}

5.model:

package com.ljf.weifuwu.springcloud.hystrix.model;

import java.math.BigDecimal;

public class HystrixUser {
    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }
}

6:controller:

package com.ljf.weifuwu.springcloud.hystrix.controller;

import com.ljf.weifuwu.springcloud.hystrix.model.HystrixUser;
import com.ljf.weifuwu.springcloud.hystrix.service.HystrixUserFegin;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import feign.Param;
import feign.RequestLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HystrixUserController {
    @Autowired
    private HystrixUserFegin huf;
    /**
     * 这个是get请求
     * @param id
     * @return
     */
    @GetMapping("/hystrix-consumer/{id}")
    @HystrixCommand(fallbackMethod = "queryUserByIdFallback")
    public HystrixUser queryInfoById(@PathVariable Long id) {
        System.out.println("hystrix的get请求!!!!!!!!!!1"+id);
        HystrixUser u= this.huf.getSingleUser(id);
        System.out.println("u:"+u.getName());
        return huf.getSingleUser(id);
    }

    /**
     * 超时异常处理
     * @param id
     * @return
     */
    public HystrixUser queryUserByIdFallback(@PathVariable Long id){
        System.out.println("超时发生错误的.....使用hystrix进行处理");
        HystrixUser u=new HystrixUser();
        u.setId(00l);
        u.setName("超时错误");
        return u;
    }
}

7.启动服务:ms-eureka-center(8761)、ms-eureka-provider(9701)、ms-hystrix-consumer(8005)

8.访问:http://localhost:8005//hystrix-consumer/1

9.将ms-eureka-provider,服务提供者,关闭之后:再次访问

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值