Zookeeper第三话 -- Springboot基于zk+dubbo实现远程服务调用

本文主要记录springboot + zk + dubbo完成远程通信服务。
坏境配置:zookeeper3.7、dubbojar包3.0.6

1.项目架构

在这里插入图片描述
就一个接口,一个实现,这里贴一下service接口类

public interface HelloService {
    String hello(String name);
}

2.yaml配置

dubbo:
  application:
    name: dubbo-service #客户端仅此名称不同
  registry:
    protocol: zookeeper
    address: 192.168.0.221:2181
    timeout: 60000
  protocol:
    name: dubbo
    port: 9999

3.pom文件

基于springboot 2.5.6

<dependencies>
	<dependency>
		<groupId>com.example</groupId>
		<artifactId>dubbo-api</artifactId>
		<version>1.0.0</version>
	</dependency>
	<dependency>
		<groupId>org.apache.dubbo</groupId>
		<artifactId>dubbo-spring-boot-starter</artifactId>
		<version>3.0.6</version>
	</dependency>
	<dependency>
		<groupId>org.apache.dubbo</groupId>
		<artifactId>dubbo-dependencies-zookeeper</artifactId>
		<version>3.0.6</version>
		<exclusions>
			<exclusion>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
			</exclusion>
		</exclusions>
		<type>pom</type>
	</dependency>
</dependencies>

4.service实现

//添加特有的dubbo服务标识 如果不添加dubbo扫描包 则需要添加Component注解交给spring管理
@DubboService
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        System.out.println("调用dubbo服务");
        return "hello my name is " + name;
    }
}

5.客户端controller接口

@RestController
public class DubboController {

	//使用dubbo注解
    @DubboReference
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        return helloService.hello(name);
    }
}

6.启动服务

需要添加@EnableDubbo注解,最后出现此日志视为成功

c.dubbo.service.DubboServiceApplication  : Started DubboServiceApplication in 39.246 seconds (JVM running for 41.205)

以上就是本章的全部内容了。

上一篇:Zookeeper第二话 – Springboot基于zk watch机制实现备用机
下一篇:Zookeeper第四话 – zookeeper高可用集群搭建

书山有路勤为径,学海无涯苦作舟

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值