springcloud学习笔记(七)Feign与Hystrix的结合使用 Hystrix(2)

首先引入依赖:

<?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">
    <parent>
        <artifactId>SpringCloudTest</artifactId>
        <groupId>com.xc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaService_feign_hystrix</artifactId>
    <dependencies>
        <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>


</project>


然后启动类加入feign和Hystrix注解,完整代码如下:

package com.xc;

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 Feign_hystrixAPP {
    public static  void main(String args[]){
        SpringApplication.run(Feign_hystrixAPP.class,args);
    }
}

写一个feign客户端

package com.xc.service;

import com.xc.entity.User;
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="cloud-test")
public interface FeignService {

    @RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
    public String sayHello(@PathVariable("words") String words);
}

feign与Hystrix结合使用十分简单,只需要给feign写一个实现类  然后在feign客户端 填写回调地址:

实现类如下:

package com.xc.service;

import org.springframework.stereotype.Component;

@Component
public class TestFallBack implements  FeignService{

    @Override
    public String sayHello(String words) {
        return "TestFallBack";
    }
}
@component注解不加  启动会报错

然后feign客户端:

package com.xc.service;

import com.xc.entity.User;
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="cloud-test",fallback = TestFallBack.class)
public interface FeignService {

    @RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
    public String sayHello(@PathVariable("words") String words);
}


写一个Rest服务:

package com.xc.controller;

import com.xc.entity.User;
import com.xc.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignHystrixController {

    @Autowired
    private FeignService feignService;

    @RequestMapping(value="/getuser/{words}",method = RequestMethod.GET)
    public String getUser(@PathVariable("words")String words){
        return feignService.sayHello(words);
    }
}


OK,测试代码已经写好,开始测试:

1,访问接口:http://localhost:7778/getuser/haha


返回正常。

2.现在我们断掉cloud-test微服务,再访问:http://localhost:7778/getuser/haha


成功跳到失败回调方法。。测试完成!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值