Spring Cloud集成Sentinel之使用OpenFein实现服务调用

Spring Cloud集成Sentinel之使用OpenFein实现服务调用

Laughing
2021-07-29 / 0 评论 / 1,027 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年05月31日,已超过232天没有更新,若内容或图片失效,请留言反馈。

Spring Cloud使用OpenFeign调用Nacos服务提供者中,我们介绍了OpenFeign调用Nacos的方式。

Spring Cloud集成Sentinel之@SentinelResource注解使用一文中,我们也介绍了@SentinelResource注解,但是注解的方式。

本文我们更进一步,介绍以下OpenFeign如何与Setinel集成。

添加依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Cloud2020</artifactId>
        <groupId>net.xiangcaowuyu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-consumer-order9003</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
    </dependencies>

</project>

修改配置文件

server:
  port: 8888
spring:
  application:
    name: nacos-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.120.180:1111
    sentinel:
      transport:
        dashboard: 192.168.120.180:9000
        port: 8719
management:
  endpoints:
    web:
      exposure:
        include: "*"
logging:
  level:
    net.xiangcaowuyu.springcloud.alibaba.service.NacosPaymentService: debug
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
nacos-provider-payment:
  ribbon:
    #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
    ReadTimeout: 5000
    #指的是建立连接后从服务器读取到可用资源所用的时间
    ConnectTimeout: 5000
    NFLoadBalancerRuleClassName: net.xiangcaowuyu.springcloud.alibaba.config.CustomerBalancerRule
  sentinel:
    enabled: true

修改启动类

@SpringBootApplication
//@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerOrderMain8888 {

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

}

定义Feign接口

@FeignClient(value = "nacos-provider-payment", fallback = NacosPaymentServiceFallback.class)
public interface NacosPaymentService {

    @GetMapping(value = "/echo/{string}")
    String echo(@PathVariable("string") String string);
}

class NacosPaymentServiceFallback implements NacosPaymentService {

    @Override
    public String echo(String string) {
        return "进入feignClient fallback了";
    }
}

定义测试接口

 @GetMapping("/echo/{id}")
    public String echo(@PathVariable("id") String id) {
        log.info("******************输出:" + id);
        return nacosPaymentService.echo(id);
    }
0

评论 (0)

取消