spring boot+dubbo+zookeeper实现RPC远程调用

POM

// An highlighted block
	<!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <!--使用zookeeper做注册中心-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

dubbo server 服务提供者 application.yml

spring:
  dubbo:
    application:
      name: provider
    registry:
      #指向zookeeper地址
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      port: 20880
    scan: cn.itcast.service

server:
  port: 9900

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

public interface IDubboDemoService {
  	String helloDubbo();
  }

service接口实现

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

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

}

@Service 来自dubbo依赖

dubbo client 服务消费者 application.yml

spring:
  dubbo:
  	application:
  		name: consumer
    registry:
    	address: zookeeper://127.0.0.1:2181
    scan: cn.itcast.service
  

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 = "1.0.0")
  public IDubboDemoService dubboDemoService;
  @Override
  public String test() {
      return    dubboDemoService.helloDubbo();
  }
}

@Reference来自 dubbo依赖

伪代码

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

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


在这里插入图片描述
java讨论群:931243010

猜你喜欢

转载自blog.csdn.net/weixin_42517093/article/details/87912776