前提

需要先安装sentinel。

父项目POM

pom.xml

<?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>
<modules>
<module>provider</module>
<module>consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzq.example</groupId>
<artifactId>cloud_sentine_open_feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud_sentine_open_feign</name>
<description>Demo project for Spring Boot</description> <packaging>pom</packaging> <properties>
<java.version>1.8</java.version>
<spring.cloud.version>Hoxton.SR8</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version>
<spring.boot.version>2.2.5.RELEASE</spring.boot.version>
</properties> <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>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> <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>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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build> </project>

主要关注:

spring-cloud-starter-alibaba-nacos-discovery

spring-cloud-starter-openfeign

spring-cloud-starter-alibaba-sentinel

服务提供者(子项目)

pom.xml

<?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>cloud_sentine_open_feign</artifactId>
<groupId>com.wzq.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>com.wzq.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies> </project>

application.yml

spring:
application:
name: cloud-sentine-open-feign-provider
cloud:
sentinel:
transport:
port: 8719
dashboard: 192.168.60.129:8080
nacos:
discovery:
server-addr: 192.168.60.129:8848
management:
endpoints:
web:
exposure:
include: "*"

ProviderApplication.java

package com.wzq.example.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
} }

HelloService.java

package com.wzq.example.api;

public interface HelloService {

    void hello();

}

HelloServiceImpl.java

package com.wzq.example.provider.api.impl;

import com.wzq.example.api.HelloService;
import org.springframework.stereotype.Service; @Service
public class HelloServiceImpl implements HelloService { int i = 0; @Override
public void hello() {
i++;
// 异常比例为0.5
if (i % 2 == 0) {
throw new RuntimeException("异常!");
}
System.out.println("hi!");
}
}

HelloController.java

package com.wzq.example.provider.controller;

import com.wzq.example.api.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @Autowired
private HelloService helloService; @RequestMapping("hello")
public void hello(){
this.helloService.hello();
} }

服务消费者(子项目)

pom.xml

<?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>cloud_sentine_open_feign</artifactId>
<groupId>com.wzq.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>consumer</artifactId> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>com.wzq.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies> </project>

application.yml

server:
port: 8081
spring:
application:
name: cloud-sentine-open-feign-consumer
cloud:
sentinel:
transport:
port: 8719
dashboard: 192.168.60.129:8080
nacos:
discovery:
server-addr: 192.168.60.129:8848
management:
endpoints:
web:
exposure:
include: "*"
feign:
sentinel:
enabled: true # Enable the Sentinel support for feign in the properties file (开启Sentinel支持属性)

ConsumerApplication.java


package com.wzq.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient
@EnableFeignClients //开启feign支持
@SpringBootApplication
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} }

HelloService.java


package com.wzq.example.service; import com.wzq.example.service.fallback.HelloServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "cloud-sentine-open-feign-provider", fallback = HelloServiceFallback.class)
public interface HelloService { @RequestMapping("hello")
void hello(); }

@FeignClient 远程调用服务提供者的接口,fallback 属性,控流和异常处理

HelloServiceFallback.java

package com.wzq.example.service.fallback;

import com.wzq.example.service.HelloService;
import org.springframework.stereotype.Component; @Component
public class HelloServiceFallback implements HelloService {
@Override
public void hello() {
System.out.println("hello Service FallBack!");
}
}

hello与接口中的方法对应,控流和异常处理

TestController.java

package com.wzq.example.controller;

import com.wzq.example.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { @Autowired
private HelloService helloService; @RequestMapping("test")
public void test(){
this.helloService.hello();
} }

sentinel控制台

进入sentinel控制台设置服务提供者,控流和降级规则就能看到效果

sentinel使用(结合OpenFeign)的更多相关文章

  1. SpringCloude简记_part3

    18. SpringCloud Alibaba Sentinel实现熔断与限流 18.1 Sentiel 官网 https://github.com/alibaba/Sentinel 中文 https ...

  2. Spring Cloud--尚硅谷2020最新版

    Spring Cloud 初识Spring Cloud与微服务 在传统的软件架构中,我们通常采用的是单体应用来构建一个系统,一个单体应用糅合了各种业务模块.起初在业务规模不是很大的情况下,对于单体应用 ...

  3. 十一. SpringCloud Alibaba

    1. SpringCloud Alibaba简介 1.1 为什么会出现SpringCloud Alibaba Spring Cloud Netflix项目进入到维护模式 什么是维护模式?=> 将 ...

  4. Sentinel与OpenFeign 服务熔断那些事

    点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 sentinel,即可免费获取源码 在上一篇中,我们讲解了 Senti ...

  5. Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现

    作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...

  6. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...

  7. Spring Cloud Alibaba(三)Sentinel之熔断降级

    本项目演示如何使用 Sentinel 完成 Spring Cloud 应用的熔断降级调用. Sentinel 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量作为切入点,从流量控制, ...

  8. SpringCloud 中集成Sentinel+Feign实现服务熔断降级

    Sentine 1.背景 Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳 ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

随机推荐

  1. IP数据包格式与ARP转发原理

    一.网络层简介1.网络层功能2.网络层协议字段二.ICMP与封装三.ARP协议与ARP欺骗1.ARP协议2.ARP欺骗 1.网络层功能 1. 定义了基于IP地址的逻辑地址2. 连接不同的媒介3. 选择 ...

  2. VBA:考场场标打印

    Function pda(x) a = x If Len(a) = 1 Then ab = "00" & a ElseIf Len(a) = 2 Then ab = &qu ...

  3. java面向对象程序设计(下)-枚举类

    在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象;再比如行星类,目前只有8个对象,这些实例有限而且固定的类,在Java中被称为枚举类 JDK1.5新增了一个enum关键字,(它与 ...

  4. Gos Log每次查询响应后自动清理临时文件,优化磁盘空间

    客户端清理 logc/controllers/file/file.go 压缩后清理原始文件 //压缩成功后 删除原文件 os.Remove(src) 返回后清理压缩文件 defer func() { ...

  5. Django模板中变量的运算

    在django中的模板下我们知道变量使用{{xxx}}来呈现,可是当出现两个变量进行运算怎么处理那? #加法: {{value|add:value2}} #返回的结果是value+value2的值,假 ...

  6. python验证码图片生成

    环境:win10(64位)+pycharm2018+pillow5.4+python3.7 对Django的跨站请求保护的有所了解的同学会知道{%csrf_token%}在实际上作用并不是那么大,只要 ...

  7. 最短路径问题 Dijkstra ——Python实现

      # 最短路径算法 Dijkstra # 输入:含权有向图 G=(V,E),V={1,2,3...n} # 输出:G中顶点 1 到各个顶点地最短距离   Dijkstra算法各点权值变化情况: 1 ...

  8. VSCode远程免密登录

    VSCode远程免密登录 本地生成密钥 生成命令如下: ssh-keygen -t rsa 会生成id_rsa, id_rsa.pub两个文件 公钥拷贝到服务器 将公钥id_rsa.pub拷贝到服务器 ...

  9. CRC校验原理和verilog实现方法(三)

    1 代码生成 verilog实现CRC校验,可以充分发挥FPGA的硬件特性,即并行运算的能力. 具体实现方式,可以参考我上一篇博客,关键是用线性反馈移位寄存器表示出多项式,另外注意校验数据高位在先.然 ...

  10. 漏洞分析:CVE 2021-3156

    漏洞分析:CVE 2021-3156 漏洞简述 漏洞名称:sudo堆溢出本地提权 漏洞编号:CVE-2021-3156 漏洞类型:堆溢出 漏洞影响:本地提权 利用难度:较高 基础权限:需要普通用户权限 ...