nacos+feign+Hystrix使用

本文介绍了如何使用Nacos作为注册中心,包括下载、启动、配置Maven依赖,实现Spring Cloud服务发现,以及通过Feign进行微服务调用和Hystrix熔断机制的应用。

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

1、使用nacos(注册中心)

1、下载

2、启动

在\nacos\bin目录下鼠标双击startup.cmd启动

3、访问测试

http://127.0.0.1:8848/nacos/index.html

账号密码都是nacos

登录成功就可以了

4、配置maven依赖

 <!--Spring Cloud 其中springcloud的版本要和springboot版本相对应-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<!--服务注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.netflix.ribbon</groupId>
                    <artifactId>ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

5、在application.properties中添加服务发现地址和自己都服务名

spring.application.name=service-vod
#nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

6、在启动类上加注解

@EnableDiscoveryClient //nocas服务注册发现

@EnableDiscoveryClient //nocas服务注册发现
public class VodApplication {
    public static void main(String[] args) {
        SpringApplication.run(VodApplication.class, args);
    }
}

2、使用feign

使用feign可以拿到注册在nacos的微服务

1、导入maven依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>0.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--服务调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

2、在启动类上加注解

@EnableFeignClients //调用nacos注册的微服务

3、使用

1、创建接口

创建接口,里面调用的远程接口跟远程微服务的控制层方法一模一样

import com.tuzhi.utilcommon.result.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @program: guli_parent
 * @description: 调用远程的Vod微服务
 * @author: 兔子
 * @create: 2022-04-16 15:24
 **/

//name=服务名称
@FeignClient(name = "service-vod")
@Component
public interface VodClient {

    //定义调用的方法路径,方法路径已经要写全
    //根据视频id删除阿里云视频
    //@PathVariable注解一定要指定参数名称,否则出错
    @DeleteMapping("/eduvod/{videoId}")
    public Result deleteVideo(@PathVariable("videoId") String videoId);

}

2、使用@Autowired注入正常使用

3、hystrix使用

熔断器,用于服务器宕机和延长请求时间

1、导入maven依赖

<!--hystrix依赖,主要是用  @HystrixCommand -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.0-jre</version>
</dependency>

2、添加application.properties配置

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

3、使用

1.在feign接口的直接上加fallback

//name=服务名称
@FeignClient(name = "service-vod", fallback = VodFileDegradeFeignClient.class)
@Component
public interface VodClient {

    //定义调用的方法路径,方法路径已经要写全
    //根据视频id删除阿里云视频
    //@PathVariable注解一定要指定参数名称,否则出错
    //删除单个视频
    @DeleteMapping("/eduvod/{videoId}")
    Result deleteVideo(@PathVariable("videoId") String videoId);

    //删除多个视频
    @DeleteMapping("/eduvod/deleteBatch")
    public Result deleteVideoBatch(@RequestParam("list") List<String> list);

2.创建一个类基础frign的接口

@Component
public class VodFileDegradeFeignClient implements VodClient {

    @Override
    public Result deleteVideo(String videoId) {
        System.out.println("执行了熔断删除视频出错了");

        return Result.error().message("删除视频出错了");
    }

    @Override
    public Result deleteVideoBatch(List<String> list) {
        System.out.println("执行了熔断删除多个视频出错了");
        return Result.error().message("删除多个视频出错了");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值