spring boot + Dubbo + Redis注册中心 实现RPC调用

spring boot + Dubbo + Redis注册中心 实现RPC调用

众所周知 dubbo推荐使用zookeeper做服务发现,但今天我们来使用另一种Redis做服务发现 这样省去了维护两种服务的经历 并且可以用作生产

POM

  <dependency>
       <groupId>com.alibaba.spring.boot</groupId>
       <artifactId>dubbo-spring-boot-starter</artifactId>
       <version>2.0.0</version>
   </dependency>
   <!--redis 依赖-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>
   
   <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.7</version>
   </dependency>
    

dubbo server 服务提供者 application.yml

spring:
  application:
    name: dubbo-producer
  dubbo:
    provider:
      threadpool: fixed
      threads: 2000
    application:
      id: server-producer
      name: server-producer
      qos-enable: true
      qos-port: 22222
      qos-accept-foreign-ip: false
    registry:
      # 使用redis 注册中心暴露服务地址
      address: redis://127.0.0.1:6379
    protocol:
      name: dubbo
      # 高效序列化 kryo, fst
      #用dubbo协议在20880端口暴露服务
      port: 20884
    #性能监控中心地址
    monitor:
      protocol: dubbo
      address: 127.0.0.1:7070
    scan: cn.itcast.service
server:
  port: 8888

dubbo服务提供者中创建service接口

public interface IDubboDemoService {
  	String helloDubbo();
  }

service接口实现

import com.alibaba.dubbo.config.annotation.Service;

@Service(version = "2.0.0")
public class IDubboDemoServiceImpl implements IDubboDemoService {
@Override
  public String helloDubbo() {
      return "hello dubbo, 我是提供者";
  }

}

dubbo client 服务消费者 application.yml

spring:
application:
  name: dubbo-consumer
dubbo:
  provider:
    threadpool: fixed
    threads: 2000
  application:
    #      id: database-consumer
    name: database-consumer
  #      qos-enable: true
  #      qos-port: 33333
  #      qos-acceptforeign-ip: false
  registry:
    #使用redis注册中心暴露服务地址
    address: redis://127.0.0.1:6379
  monitor:
    #      protocol: dubbo
    address: 127.0.0.1:7070
server:
port: 9988
  

dubbo client 服务消费者service接口要与提供者接口名称相同

public interface IDubboDemoService {
  String helloDubbo();
}	
@Service
public class IDubboDemoServiceImpl implements IDubboDemoService {
  
  @Override
  //该内容随便写
  public String helloDubbo() {
      return "hello dubbo, I'm server!";
  }
  }

@Service 来自spring依赖

创建自己的接口

public interface IDemoService {
  String test();
}

@Service
public class DemoServiceImpl implements IDemoService {
  //调用dubbo服务
 @Reference(version = "2.0.0")
  public IDubboDemoService dubboDemoService;
  @Override
  public String test() {
      return    dubboDemoService.helloDubbo();
  }
}

启动类加上@EnableDubboConfiguration

这是使用dubbo-spring-boot-starter的新特性 必须要有

@Reference来自 dubbo依赖

伪代码

@Autowired(required = false)
private IDemoService demoService;
@GetMapper("/get")
demoService.test

即可返回“hello dubbo, 我是提供者”

gitee项目地址
https://gitee.com/scriptcode/dubbo-example.git

java讨论群:931243010

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值