第十一节 OpenFeign与Hystrix的结合使用

亮子 2022-07-05 08:34:22 17216 0 0 0

1、添加依赖

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
<!--            <version>2.2.2.RELEASE</version>-->
        </dependency>
        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、修改配置文件

## 启动熔断器
feign.hystrix.enabled=true

3、在启动类上加注解

package com.shenmazong.ware;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ServerShopWareApplication {

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

4、定义熔断的实现类

@Component
public class MessageServiceImpl implements MessageService {

    @Override
    public ResultResponse sendMessage(@RequestBody MessageVo messageVo) {
        System.out.println("sendMessage: 熔断了");
        return ResultResponse.FAILED(500, "发生熔断了");
    }
}

5、设置熔断实现类

@FeignClient(value = "server-shop-mq", fallback = MessageServiceImpl.class)
@Component
public interface MessageService {

    @PostMapping(value = "/message/sendMessage")
    ResultResponse sendMessage(@RequestBody MessageVo messageVo);

}

6、测试运行

图片alt

7、没有注册中心的feign

1)、定义一个bean,控制设备的入参

public class ControlParam{
    // 设备id
    String applianceId;
    // 控制的命令
    Map command;
}

2)、在@FeignClient注解中,定义设备控制服务的地址,本demo定义为本地服务的8081端口。在client目前只有设备控制的接口,可以根据业务的需要进行扩展,比如需要设备状态查询接口。

@FeignClient(name = "example-service", url = "http://localhost:8081")
public interface ExampleClient {

    @PostMapping("/device/control")
    String control(ControlParam controlParam);
}

3)、在启动类上添加注解

@SpringBootApplication
@EnableFeignClients
public class DemoApplication {

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

4)、在后端服务中暴露接口,提供给APP使用

@RestController
public class ExampleController {

    @Autowired
    private ExampleClient exampleClient;

    @PostMapping("/control")
    public String contrlExample(@RequestBody ControlParam controlParam) {
        return exampleClient.control(controlParam);
    }
}

如上,通过4个步骤就可以使用OpenFeign实现服务间的接口调用。其具体访问路径为:

APP访问Controller暴露的接口“/control”接口
在contrlExample方法中通过ExampleClient去调用设备控制服务。

参考文档