栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合dubbo简单实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合dubbo简单实例

回顾了下以前学过的dubbo,使用最新的springboot与dubbo简单搭建了实例,借此巩固下所学知识。

  • 官方文档
  • 示例

环境:springboot2.5.5、dubbo3.0.3、zookeeper3.7.0、maven3.6.3、jdk8、idea2020.3.4

1、首先新建空的maven父项目用于管理版本,各版本依赖如下



    4.0.0
    pom
    
        provider-service
        consumer-service
        interfaces
    

    com.example
    dubbo-learning
    0.0.1-SNAPSHOT
    dubbo-learning
    dubbo
    
        1.8
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.5.5
                pom
                import
            
            
            
                org.apache.dubbo
                dubbo-dependencies-zookeeper
                3.0.3
                pom
            
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                3.0.3
            
            
            
                com.example
                interfaces
                0.0.1-SNAPSHOT
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.5.5
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


2、新建提供者模块:provider-service

pom.xml



    
        dubbo-learning
        com.example
        0.0.1-SNAPSHOT
    
    4.0.0

    provider-service
    
        8
        8
    
    
        
            com.example
            interfaces
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.projectlombok
            lombok
            true
            provided
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            
            pom
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        
    

application.yml

server:
  port: 8001
spring:
  application:
    name: provider-service
dubbo:
  scan:
    base-packages: com.example.service # 指定需要注册到注册中心的服务扫描路径,也可以使用注解@EnableDubbo指定
  protocol:
    name: dubbo
    port: -1 # random
    host: 192.168.0.100 # 多网卡时需要指定一下取本地的哪个ip,尤其是有虚拟网卡的时候
  registry:
    address: zookeeper://192.168.192.99:2181

提供者就两个类,一个启动类和一个OrderService类。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApp {

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

需要注册到注册中心的提供者服务,在旧版本中使用dubbo的@Service注解,很容易与spring的弄混,新版本中用的是@DubboService注解,消费者引用的时候用的是@DubboReference,不再使用@Reference注解。

package com.example.service;

import com.example.pojo.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service // 注,这是spring的注解,不是dubbo的
@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Override
    public OrderInfo getOrder(String userName) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(100L);
        orderInfo.setAmount(new BigDecimal("10000.23"));
        orderInfo.setDate(new Date());
        orderInfo.setUserName(userName);
        log.info(">>> provider-serevice被调用了...");
        return orderInfo;
    }
}

3、服务消费者:consumer-service

pom.xml



    
        dubbo-learning
        com.example
        0.0.1-SNAPSHOT
    
    4.0.0

    consumer-service

    
        8
        8
    

    
        
            com.example
            interfaces
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            true
            provided
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            
            pom
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        
    


application.yml

server:
  port: 8002
spring:
  application:
    name: comsumer-service
dubbo:
  registry:
    address: zookeeper://192.168.192.99:2181 # 消费者可以不指定协议等信息

主启动类及测试controller

package com.example;

import com.example.pojo.orderInfo;
import com.example.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class UserApp {

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

    
    @RestController
    @RequestMapping("user")
    @Slf4j
    public static class UserController {
        
        @DubboReference(check = false, methods = {@Method(name = "getOrder", timeout = 5000)})
        private OrderService orderService;

        @GetMapping("getOrderByUserName")
        public OrderInfo getOrderByUserName(@RequestParam("userName") String userName) {
            // 调用远程服务测试
            OrderInfo orderInfo = orderService.getOrder(userName);
            log.info(">>> dubbo 远程调用成功:{}", orderInfo);
            return orderInfo;
        }
    }
}

简单的一个入门案例就完成了。

主要在于maven依赖,依赖正确,就算成功一半了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号