SpringCloud中使用Hystrix和OpenFeign远程调用接口服务

本文详细介绍了如何使用Spring Cloud Alibaba的Nacos实现微服务生产者(服务提供者)和服务消费者(服务调用者)的集成,包括服务注册、配置设置、依赖引入和OpenFeign的远程调用。重点展示了如何通过@EnableDiscoveryClient和@EnableFeignClients进行服务发现与故障转移机制的应用。

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

1、微服务接口提供端(生产者)

(1)pom文件,需要加入SpringCloud Alibaba的Nacos依赖,将服务实例注册到Nacos微服务注册中心

 <!--服务注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

(2)在application.properties配置文件中配置服务实例名称和Nacos服务地址

# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=localhost:8848

# 环境设置:dev、test、prod
spring.profiles.active=dev

(3)在主启动类上加上@EnableDiscoveryClient注解

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class VodApplication {

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

}

(4)在controller中提供服务接口

//    根据videoId删除阿里云视频
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteAlyVideo(@PathVariable("id") String id){
        vodService.deleteAlyVideo(id);
        return R.ok();
    }

2、微服务接口调用端(消费者)

(1)在pom.xml文件中加上Nacos、Hystrix和openFeign的依赖

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

        <!--hystrix依赖,主要是用  @HystrixCommand -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--服务注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--服务调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

(2)在application.properties配置文件中开启Hystrix和Nacos服务注册地址

# 服务端口
server.port=8001
# 服务名
spring.application.name=service-edu

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=localhost:8848

#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000

# 环境设置:dev、test、prod
spring.profiles.active=dev

(3)在主启动类上加上@EnableDiscoveryClient和@EnableFeignClients注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EduApplication {

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

(4)编写OpenFeign远程调用的接口所在服务实例的名称和接口的地址,

@FeignClient(value = "service-vod",fallback = VodFileDegradeFeignClient.class)
@Component
public interface VodClient {

    @DeleteMapping("/eduvod/video/deleteVideo/{id}")
    public R deleteAlyVideo(@PathVariable("id") String id);

    //    删除多个阿里云视频
    @DeleteMapping("/eduvod/video/deleteMoreVideo")
    public R deleteMoreVideo(@RequestParam("ids") List<String> ids);
}

fallback = VodFileDegradeFeignClient.class是当调用远程服务时,远程提供的接口出现问题或者服务宕机,就会调用本地的VodFileDegradeFeignClient类实现VodClient接口中的方法,比如:当调用远程服务deleteAlyVideo接口出现问题,就会执行VodFileDegradeFeignClient类中的deleteAlyVideo方法

@Component
@Slf4j
public class VodFileDegradeFeignClient implements VodClient {
    @Override
    public R deleteAlyVideo(String id) {
        log.info("hystrix熔断器执行了,deleteAlyVideo...");
        return R.error().message("time out");
    }

    @Override
    public R deleteMoreVideo(List<String> ids) {
        log.info("hystrix熔断器执行了,deleteMoreVideo...");
        return R.error().message("time out");
    }
}

(5)在Controller中使用OpenFeign调用远程服务提供的接口

 	@Autowired
    EduVideoService eduVideoService;	
	@Autowired
    VodClient vodClient;

//    根据小节id删除小节信息和阿里云视频信息
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideoById(@PathVariable("id") String id){
//        1、使用openFeign调用阿里云视频微服务删除阿里云视频
        EduVideo eduVideo = eduVideoService.getById(id);
        log.info("eduVideo:{}",eduVideo);
        String videoSourceId = eduVideo.getVideoSourceId();
        log.info(videoSourceId);
        //判断小节里面是否有视频id
        if(!StringUtils.isEmpty(videoSourceId)) {
            //根据视频id,远程调用实现视频删除
            R result = vodClient.deleteAlyVideo(videoSourceId);
            if(result.getCode() == 20001) {
                throw new MyException(20001,"删除视频失败,熔断器...");
            }
        }
//        2、删除小节信息
        eduVideoService.removeById(id);
        return R.ok();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值