Feign的理解
Feign是什么?
Feign是⼀个http请求调⽤的轻量级框架,也可以说是声明式WebService客户端
Feign的作⽤
可以以Java接⼝注解的⽅式调⽤Http请求,它使java调⽤Http请求变的简单
Feign集成了Ribbon,实现了客户端的负载均衡
Feign的⼯作原理(简易版)
1、⾸先通过@EnableFeignCleints注解开启FeignCleint
2、根据Feign的规则实现接⼝,并加@FeignCleint注解
3、程序启动后,会进⾏包扫描,扫描所有的@FeignCleint的注解的类,并将这些信息注⼊到ioc容器中。
4、当接⼝的⽅法被调⽤,通过jdk的代理,来⽣成具体的RequesTemplate
5、RequesTemplate再⽣成Request
6、Request交给Client去处理,其中Client默认是HttpUrlConnection(也可以是HttpClient或Okhttp,需要配置)
7、最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloudflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class JisServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(JisServiceConsumerApplication.class, args);
}
}
根据Feign的规则实现的接⼝
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
spring framework是什么框架的
import org.springframework.cloudflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class JisServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(JisServiceConsumerApplication.class, args);
}
}
熔断器的fallback(调⽤接⼝失败时会执⾏)import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
ample.jisserviceconsumer.inter.HelloRemote;
@Component
public class HelloRemoteHystrix implements HelloRemote{
@Override
public String hello(@RequestParam(value = "name") String name) {
return "hello" +name+", this messge send failed biu biu biu ~ ";
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论