Spring Cloud(十)Hystrix实战

本文介绍如何在Spring Cloud中配置Eureka Server作为服务注册中心,并创建Eureka Client服务提供者和服务消费者。此外,还介绍了如何利用Hystrix实现服务间的熔断保护,确保系统的稳定性和可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前期准备

1、创建一个Eureka Server工程,项目名称:eureka-server

2、创建一个Eureka Client 服务提供者 工程,项目名称:eureka-client1

工程eureka-server和eureka-client1 创建请参考

Spring Cloud(三)Eureka实战

1、启动Eureka 服务提供者、Eureka注册中心

启动eureka-server、eureka-client1

PS:eureka-client1 在idea中通过多实例启动

在这里插入图片描述

查看 Eureka 控制台

在这里插入图片描述

2、创建并启动服务消费者

项目名称:service-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.yi</groupId>
    <artifactId>serice-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serice-hystrix</name>
    <description>serice-hystrix</description>

    <parent>
        <groupId>com.yi</groupId>
        <artifactId>finchley</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
    </dependencies>

</project>

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-hystrix


@EnableHystrix
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@EnableHystrix注解开启Hystrix

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloHystrix")
    public String helloService(String name) {
        return restTemplate.getForObject("http://SERVICE-CLIENT/hello?name=" + name, String.class);
    }

    public String helloHystrix(String name) {
        return "hello :"+name+",服务繁忙,稍后再试!";
    }
}

@HystrixCommand :提供熔断器的功能,并指定了fallbackMethod熔断方法

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hello")
    public String hello(@RequestParam String name) {
        return helloService.helloService(name);
    }
}

访问:http://localhost:876/hello?name=yi

1、如果service-client 服务正常启动,

轮询出现,与前章节ribbon一致

hello yi ,port is 8762
hello yi ,port is 8763

2、如果service-client 服务关闭,出现

hello : yi,服务繁忙,稍后再试!

3、Feign结合使用Hystrix

Feign整合了Hystrix,具有熔断的能力。

Spring Cloud(八)Feign实战(hystrix断路器)


git项目源码:https://github.com/yilei111/-Spring-Cloud-Finchley

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值