简单使用SpringBoot+Dubbo+Zookeeper

在查阅了Dubbo的官方文档后,对注解的实现做了一个小小的总结

官方文档:http://dubbo.apache.org/zh-cn/docs/user/configuration/annotation.html

 

服务提供方

1、添加apache的Dubbo和Zookeeper依赖 

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.1</version>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.1</version>
    <type>pom</type>
</dependency>

           

2、创建配置文件yml

server:
  port: 8080
dubbo:
  application:
    name: samples-annotation-provider
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
port: 20880

 

3、在启动类添加注解 @EnableDubbo 

  import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

  注:scanBasePackages=“包路径”,也可以只扫描指定的包路径下的文件

package com.hai.dubbo.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {

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

}

 

4、实现提供的接口,并在实现类添加注解  @Service

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

package com.hai.dubbo.provider.service.impl;

import org.apache.dubbo.config.annotation.Service;
import com.hai.dubbo.api.service.HelloService;

@Service
public class HelloServiceImpl implements HelloService {

	@Override
	public String sayHello(String name) {
		return "你好:" + name;
	}

}

 

服务消费方

5、添加相关依赖,同步骤 1

 

6、创建配置文件 yml

server:
  port: 8088
dubbo:
  application:
    name: samples-annotation-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol: 
    name: dubbo

 

7、在启动类添加注解,同步骤 3

package com.hai.dubbo.consumer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {

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

}

 

8、引用远程调用的接口,使用注解 @Reference 注入

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

package com.hai.dubbo.consumer.web;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hai.dubbo.api.service.HelloService;

@RestController
public class ConsumerController {
	
	@Reference
	private HelloService helloService;
	
	@GetMapping("hello")
	public String sayHello(String name){
		return helloService.sayHello(name);
	}

}

 

启动zookeeper --> 启动服务提供方 --> 启动服务消费方 -->访问消费方:

浏览器中输入:localhost:8089/hello?name=dubbo

 

注意:

1、dubbo-dependencies-zookeeper的版本是2.7.1,对应使用的zookeeper的版本是3.4.13,指向注册ip部署的zookeeper版本也要一致,据说会报错,尝试了3.4.13,连接本地3.4.14版本的zookeeper,发现并没有报错

2、需要添加web的依赖,不然启动之后,没有监听指定端口,服务会直接结束

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、需要把待实现的接口抽成一个jar包引入至工程,因为服务提供方注册服务的时候,是用 包名 + 类名 来标记的,服务消费方引用时也是,如果包名不一致,会找不到服务,报以下错误:

No provider available for the service com.hai.dubbo.consumer.service.HelloService

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值