SpringCould2.x Greenwich版本搭建:(三)使用hystrix熔断器

上一章:SpringCould2.x Greenwich版本搭建:(二)使用Feign调用

1.创建Module项目springcould-hystrix

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itlinli</groupId>
    <artifactId>springcould-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcould-hystrix</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.itlinli</groupId>
        <artifactId>springcould-2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

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

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

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

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

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>

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

</project>

2.配置文件

#服务名称
spring.application.name=consumer-hystrix
#端口号
server.port=8505
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/

#feign开启熔断
feign.hystrix.enabled=true

#feign可以用okhttp代替传统的httpclient,性能更好
feign.httpclient.enabled=false
feign.okhttp.enabled=true

3.主程序

@SpringBootApplication
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringcouldHystrixApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
   
     @Bean
    public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
        HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean;
    }
}

4.service和impl

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "error")
    public String hello(String name) {
        return restTemplate.getForObject("http://producer/get?name="+name, String.class);
    }

    public String error(String name) {
        return "hi,"+name+",sorry,error!";
    }
}
@FeignClient(value = "producer",fallback = UserFeginFailBackImpl.class)//feign使用熔断器
public interface SchedualService {
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    String hello(@RequestParam(value = "name") String name);
}
@Component
public class UserFeginFailBackImpl implements SchedualService {

    @Override
    public String hello(String name) {
        System.out.println("熔断,默认回调函数");
        return "{\"id\":-1,\"name\":\"熔断用户\",\"msg\":\"请求异常,返回熔断用户!\"}";
    }
}

5.控制层controller

@RestController
public class HelloController {
    @Autowired
    SchedualService schedualService;

    @Autowired
    HelloService helloService;
    
    @RequestMapping(value = "/hello/{name}")
    public String hello(@PathVariable(value = "name") String name)
    {
        return helloService.hello( name );
    }

    @RequestMapping(value = "/fegin/{name}")
    public String hello2(@PathVariable(value = "name") String name){
        return  schedualService.hello(name);
    }
}

6.启动服务,访问正常,把提供者2个进程给停掉,再次调用。

7.访问http://localhost:8505/hystrix.stream

8.访问http://localhost:8505/hystrix

9.断路聚合Hystrix Turbine将各个微服务中使用的熔断器集中到一起来

添加依赖

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

10.修改配置文件

#添加使用熔断器的模块在eureka中的服务名称,我这里只写了一个
turbine.appConfig= consumer-hystrix,xxx               
turbine.clusterNameExpression= new String("default")

11.启动类添加注解

@EnableTurbine

12.访问http://localhost:8050/actuator/hystrix.stream

13.访问:http://localhost:8505/hystrix 添加http://localhost:8050/actuator/hystrix.stream进入

下一章:SpringCould2.x Greenwich版本搭建:(四)使用Zuul网关

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值