openFeign服务调用

openFeign设计原理

openFeign是轻量级的Http请求框架,不再像restTemplate那样显式声明请求URL,参数,返回类型,直接通过模块化的思想,将服务调用层封装为一个模块,在controller层像调用普通service那样调用,相对更加直观。封装了Http调用流程,更适合面向接口化的变成习惯

传统http请求流程

类似httpclient,restTemplate,OkHttp都是如此

openFeign流程

在Feign 底层,通过基于面向接口的动态代理方式生成实现类,将请求调用委托到动态代理实现类,基本原理如下所示:

实践

引入依赖

        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

主启动类加注解

@EnableFeignClients
@SpringBootApplication
@EnableEurekaClient //声明自己是eureka客户端
public class Order8102 {

    public static void main(String[] args) {
        SpringApplication.run(Order8102.class,args);
    }

}

服务调用层

@Component
@FeignClient(value = "PAYMENT8101")//声明服务名
public interface FeignService {

    @RequestMapping("/res/res")//controller方法的接口声明
    String create();
}

controller服务调用就想正常的调用service的方法即可

@RestController
@Slf4j
@RequestMapping("/get")
public class OrderController {

    @Autowired
    private FeignService feignClient;

    @RequestMapping("/get")
    public String create(){
        return feignClient.create();
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_41922289/article/details/106701562