一、什么是feign
1、我们在微服务之间接口的相互之间调用;主要作用于controller层(业务层);降低代码的冗余,主要是集成了负载均衡,主要是以接口的方式发送Http请求。主要是远程服务调用;
二、feign的使用
1.首先我们需要引入对应的java包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
2.定义一个接口,在接口上面添加@FeignClient注解,在注解的后面主要是指定我们的服务名称,之后通过里面的接口路径找到对应的服务,进行远程调用;
@FeignClient(value = oAuth, contextId = "user",configuration = FeignConfiguration.class)
public interface BuildingApi {
@PostMapping("user")
String add(UserBO userBO);
}
3.在远程调用的服务里面的启动类上面添加@EnableFeignClients注解,之后spring加载@FeignClient就能找到对应的服务;
@SpringBootApplication//启动springboot类加载、自动装配原理
@EnableFeignClients//feign服务加载
@MapperScan("com.netrust.*.dao")//注解扫描mapper包位置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
基础使用就到这里,接下来,将为大家介绍一下用到熔断监测、降级和恢复机制 Hystrix