springboot整合dubbo实现远程过程调用的简单案例

说明

springboot整合dubbo实现远程调用的小demo,dubbo以nacos作为注册中心。

一共3个模块

第一个模块提供api接口。

第二个模块提供接口实现类。

第三个模块远程调用第二个模块的服务。


模块一:sample-api

只定义接口interface。

public interface IHelloService {
    String sayHello(String name);
}

模块二:sample-provider

1、依赖模块一,实现interface。并在实现类上添加来自Dubbo的注解@Service,用来发布服务。

import org.apache.dubbo.config.annotation.Service;

@Service
public class HelloServiceImpl implements IHelloService {
    @Value("${dubbo.application.name}")
    private String serviceName;

    @Override
    public String sayHello(String name) {
        return String.format("[%s]: Hello,%s", serviceName, name);
    }

}

2、在application.yml添加dubbo的配置。

dubbo:
  application:
    name: springboot-provider
  protocol:
    name: dubbo
    port: 20880
  #注册中心设置为nacos
  registry:
    address: nacos://192.168.31.200:8001
  provider:
    timeout: 1000

3、主启动类添加注解@DubboComponentScan

@DubboComponentScan
@SpringBootApplication
public class SampleProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleProviderApplication.class, args);
    }
}

4、启动即可

模块二的依赖

<dependencies>
    <!--springboot-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.3</version>
    </dependency>
	<!--nacos-->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-discovery-spring-boot-starter</artifactId>
        <version>0.2.4</version>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba.spring</groupId>
                <artifactId>spring-context-support</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--dubbo-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.5</version>
    </dependency>
    <!--引入接口interface模块-->
    <dependency>
        <groupId>com.chw</groupId>
        <artifactId>sample-api</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

模块三:consumer

1、添加模块一的依赖获得interface接口;

2、使用Dubbo提供的@Reference来获得远程代理对象;

public class Consumer {
    //使用Dubbo提供的@Reference来获得一个远程代理对象
    @Reference(url = "dubbo://192.168.31.200:20880/com.chw.server.sampleapi.service.IHelloService")
    private IHelloService helloService;

    public static void main(String[] args) {
        helloService.sayHello("nihao");
    }
}

依赖的是模块一,调用到的是模块二的服务。

模块三的依赖

<dependencies>
    <!--dubbo-->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.5</version>
    </dependency>
    <!--引入接口interface模块-->
    <dependency>
        <groupId>com.chw</groupId>
        <artifactId>sample-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--springboot-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.7.3</version>
    </dependency>
</dependencies>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值