Fiegn 声明式接口调用、Hystrix 容错监控机制

Fiegn 声明式接口调用

什么是Feign

Netflix,Feign 是⼀个提供模版的声明式 Web Service 客户端,使用 Feign 可以简化 Web Service 客户端的编写,开发者可以通过简单的接口和注解来调用 HTTP API。
Spring Cloud Feign:可插拔、基于注解、负载均衡、服务熔断
只需要创建接口,同时在接口上添加相关注解即可。
Ribbon 和 Feign 的区别:
Ribbon 是⼀个通用的 HTTP 客户端⼯具,Feign 是基于 Ribbon ,更加灵动

Feign 的特点:

1、Feign 是⼀个声明式 Web Service 客户端
2、支持 Feign 注解、Spring MVC 注解
3、Feign 基于 Ribbon 实现
4、Feign 集成了 Hystrix

1、创建 Module,pom.xml

<dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>
</dependencies>

2、application.yml

server:
 port: 8050
spring:
 application:
 name: feign
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8761/eureka/
 instance:
 prefer-ip-address: true

3、启动类

package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
 public static void main(String[] args) {
 SpringApplication.run(FeignApplication.class,args);
 }
}

4、实体类

package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
 private Integer id;
 private String name; }

5、FeignProviderClient 接口

package com.southwind.feign;
import com.southwind.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@FeignClient(value = "provider")
public interface FeignProviderClient {
 @GetMapping("/provider/findAll")
 public Collection<Student> findAll();
 @GetMapping("/provider/findById/{id}")
 public Student findById(@PathVariable("id") Integer id);
 @PostMapping("/provider/save")
 public void save(@RequestBody Student student);
 @PutMapping("/provider/update")
 public void update(@RequestBody Student student);
 @DeleteMapping("/provider/deleteById/{id}")
 public void deleteById(@PathVariable("id") Integer id);
}

6、FeignHandler

package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.feign.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/feign")
public class FeignHandler {
 @Autowired
 private FeignProviderClient feignProviderClient;
 @GetMapping("/findAll")
 public Collection<Student> findAll(){
 return feignProviderClient.findAll();
 }
  @GetMapping("/findById/{id}")
 public Student findById(@PathVariable("id") Integer id){
 return feignProviderClient.findById(id);
 }
 @PostMapping("/save")
 public void save(@RequestBody Student student){
 feignProviderClient.save(student);
 }
 @PutMapping("/update")
 public void update(@RequestBody Student student){
 feignProviderClient.update(student);
 }
 @DeleteMapping("/deleteById/{id}")
 public void deleteById(@PathVariable("id") Integer id){
 feignProviderClient.deleteById(id);
 }
}

Hystrix 容错监控机制

什么是微服务的容错机制

提前预设解决方案,系统进行自主调节,遇到问题及时处理
什么是 Hystrix
Netflix
设计原则
1、服务隔离机制
2、服务降级机制
3、熔断机制
4、提供实时的监控和报警功能
5、提供实时的配置修改功能

1、创建 Module,pom.xml

<dependencies>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 </dependency>
</dependencies>

2、application.yml

server:
 port: 8060
spring:
 application:
 name: hystrix
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8761/eureka/
 instance:
 prefer-ip-address: true
feign:
 hystrix:
 enabled: true
management:
 endpoints:
 web:
 exposure:
 include: 'hystrix.stream'

3、创建启动类

package com.southwind;
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 HystrixApplication {
 public static void main(String[] args) {
 SpringApplication.run(HystrixApplication.class,args);
 }
}

4、实体类

package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
 private Integer id;
 private String name; }

5、FeignProviderClient 接口

package com.southwind.feign;
import com.southwind.entity.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@FeignClient(value = "provider")
public interface FeignProviderClient {
 @GetMapping("/provider/findAll")
 public Collection<Student> findAll();
 @GetMapping("/provider/findById/{id}")
 public Student findById(@PathVariable("id") Integer id);
 @PostMapping("/provider/save")
 public void save(@RequestBody Student student);
 @PutMapping("/provider/update")
 public void update(@RequestBody Student student);
 @DeleteMapping("/provider/deleteById/{id}")
 public void deleteById(@PathVariable("id") Integer id);
}

6、HystrixHandler

package com.southwind.controller;
import com.southwind.entity.Student;
import com.southwind.feign.FeignProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
@RequestMapping("/hystrix")
public class HystrixHandler {
 @Autowired
 private FeignProviderClient feignProviderClient;
 @GetMapping("/findAll")
 public Collection<Student> findAll(){
 return feignProviderClient.findAll();
 }
 @GetMapping("/findById/{id}")
 public Student findById(@PathVariable("id") Integer id){
 return feignProviderClient.findById(id);
 }
 @PostMapping("/save")
 public void save(@RequestBody Student student){
 feignProviderClient.save(student);
 }
 @PutMapping("/update")
 public void update(@RequestBody Student student){
 feignProviderClient.update(student);
 }
 @DeleteMapping("/deleteById/{id}")
 public void deleteById(@PathVariable("id") Integer id){
 feignProviderClient.deleteById(id);
 }
}

数据监控 URL:http://localhost:8060/actuator/hystrix.stream

7、添加可视化界面组件

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

8、启动类添加相应注解

package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import
org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {
 public static void main(String[] args) {
 SpringApplication.run(HystrixApplication.class,args);
 }
}

可视化界面URL:http://localhost:8060/hystrix
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走不尽的心路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值