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