openfeign和dubbo远程调用

openfeign和dubbo远程调用

⭐️使用openfeign

消费端:

1.导入依赖

2.启动类加@EncableFeignClients注解(开启openfeign)

3.编写feign接口加上@FeignClient注解(绑定服务提供方)

4调用接口

  • 1.依赖

    
        <dependencies>
            <!--openfeign-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--hystrix-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <!--eureka client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!--web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--一般基础通用配置-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
  • 2.主启动类添加注解

    @SpringBootApplication
    @EnableFeignClients
    @EnableHystrix
    public class OpenFeignApp
    {
        public static void main(String[] args)
        {
            SpringApplication.run(OrderHystrixMain80.class,args);
        }
    }
    
    
  • 3.feign接口

    @Component
    @FeignClient("SERVICE-PROVIDER")
    public interface ProductFeignService {
        /**
         * 编写技巧,直接把服务提供者那一端的控制器方法的两行拷贝过来,最后加一个分号就可以了,别的什么也不用动
         */
        //通过控制层url进行帮订。
        @RequestMapping("/provider/product/findAll")
        public List<Product> findAll();
    
        @RequestMapping("/provider/product/findByPid")
        public String findByPid(@RequestParam("pid") Integer pid);
    }
    
    
  • 4.另外的服务调用接口


@RestController
@Slf4j
public class PaymentController {
    /**
     * 使用 Feign 方式调用
     */
    @Resource
    private ProductFeignService productFeignService; // 注入 service 中 通过 @FeignClient 定义好的接口

    @GetMapping("/feign/payment/get/{id}")
    public RespResult<Payment> getPayment(@PathVariable("id") Integer id) {
        log.info("<<<<<<<<<<<  我是通过feign >>>>>>>>>>>>");
        return productFeignService.findByPid(id);
    }
}

⭐️ 使用Dubbo远程调用

1.生产者消费者添加dubbo依赖

2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service

3.消费者使用@Refence注解注入Service

  • 1.生产者消费者添加dubbo依赖
    <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
  • 2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service

    //dubbo里的@Service注解
    import org.apache.dubbo.config.annotation.Service;
    
    @Service
    public class AppServiceImpl implements IAppService {
        @Autowired
        AppMapper appMapper;
    
    
        @Override
        public AppDTO createApp(AppDTO appDTO) throws BizException {
         
    
            return null;
        }
    
    
    
  • 3.消费者使用@Refence注解注入Service

@Slf4j
@RestController
public class AppController {
//远程调用
    @Reference
    IAppService appService;

    public AppDTO createApp(@RequestBody AppVo appVo){

        return appService.createApp(appDTO);
    }

### Dubbo远程调用OpenFeign功能及适用场景对比 #### 功能特性比较 Dubbo 是一种高性能的 Java RPC 框架,支持多种协议(如 Dubbo 协议、HTTP 协议等),主要用于实现服务间的高效通信[^3]。它采用基于接口的远程方法调用机制,适用于大规模分布式系统的内部服务间通信。 相比之下,OpenFeign 是 Spring Cloud 提供的一个声明式 HTTP 客户端工具,用于简化 RESTful API 的调用过程[^2]。它的核心优势在于简单易用,开发者可以通过定义接口并添加注解来完成服务之间的交互,无需编写复杂的 HTTP 请求代码。 #### 性能表现 在性能方面,由于 Dubbo 默认使用高效的二进制协议(如 Dubbo 自带的私有协议或 gRPC 等),其传输效率通常高于基于 HTTP/JSON 的 OpenFeign。对于高并发、低延迟的服务调用需求,Dubbo 更具竞争力。 然而,在轻量级应用场景下,OpenFeign 凭借其简洁的设计良好的生态集成能力(尤其是与 Spring Boot Spring Cloud 的无缝配合),能够显著降低开发成本,并提供足够的性能保障。 #### 适用场景分析 - **Dubbo** 适合于强依赖关系复杂的企业级应用环境,尤其是在需要跨多个子系统进行高频次数据交换的情况下。例如金融交易处理平台中的订单管理系统可能频繁访问账户余额查询模块,则可考虑引入 Dubbo 来优化这类操作的速度与稳定性[^5]。 - **OpenFeign** 更倾向于松耦合程度较高的互联网产品设计模式之中,比如电商平台的商品详情页加载过程中可能会异步获取评论列表等内容片段;此时利用 Feign 可快速构建起这些外部资源抓取逻辑而不会增加太多额外负担。 #### 配置方式差异 从配置角度来看,两者也有明显区别: - 对于 Dubbo 而言,除了基本的服务注册发现外还需要指定具体的协议类型以及监听端口号等相关参数设置; - 而 OpenFeign 则更加注重约定优于配置的理念,大多数情况下只需关注目标 URL 地址即可自动完成整个请求链路组装工作[^4]。 以下是两种技术栈下的典型代码示例: ```java // Dubbo Consumer Example public interface DemoService { String sayHello(String name); } @Reference(url = "dubbo://127.0.0.1:20880") private DemoService demoService; @RequestMapping("/hello/{name}") public String hello(@PathVariable String name){ return this.demoService.sayHello(name); } ``` ```java // OpenFeign Client Example @FeignClient(name="example-service", url="http://localhost:8080") public interface ExampleClient { @GetMapping("/greeting") public String getGreeting(); } @RestController public class GreetingController { private final ExampleClient exampleClient; public GreetingController(ExampleClient exampleClient) { this.exampleClient = exampleClient; } @GetMapping("/fetch-greeting") public ResponseEntity<String> fetchGreeting() { return new ResponseEntity<>(this.exampleClient.getGreeting(), HttpStatus.OK); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

suheng-1222

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

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

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

打赏作者

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

抵扣说明:

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

余额充值