一.安装并启动Zookeeper
看看这篇博文吧,我就不赘述了 https://blog.csdn.net/lisongjia123/article/details/78639242
二.创建公共接口
-
新建一个maven项目
-
创建接口:
package com.zc.dubbo.service;
public interface DemoService {
String sayHello(String name);
}
-
把项目打包成jar包
打包jar包
三.创建服务提供者
- 先创建一个空的springboot项目,加入
dubbo-spring-boot-starter
依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
-
导入之前生成的公共接口jar包(不会导入自行百度)
image.png
3.实现公共接口,并添加dubbo的@service(注意不是spring的service注解,看导包)
实现类:
package com.zc.dubboprovider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.zc.dubbo.service.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}
- 配置文件
先把properties文件修改为yml文件(习惯使用yml文件,也可不改)
#dubbo协议
dubbo:
protocol:
id: dubbo
name: dubbo
port: 12345
status: server
#注册中心地址
registry:
address: zookeeper://localhost:2181
#dubbo应用名
application:
name: dubbo-provider-demo
- 在启动类上开启dubbo服务并扫描服务类
package com.zc.dubboprovider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.zc.dubboprovider.service.impl")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
- 启动生产者
四.创建服务消费者
- 同样新建springboot项目,加入
dubbo-spring-boot-starter
依赖,消费者要测试访问所以加入web依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
-
导入公共接口的依赖
-
配置文件
server:
port: 8080
dubbo:
protocol:
id: dubbo
name: dubbo
registry:
address: zookeeper://localhost:2181
application:
name: dubbo-consumer-demo
- 编写controller类,并使用dubbo的 @Reference 注入公共接口,dubbo会自动去注册中心找相应服务的生产者,并通过dubbo协议调用相应方法
package com.zc.dubboconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zc.dubbo.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@RestController
public class DemoController {
@Reference
private DemoService demoService;
@RequestMapping("/sayHello")
public String sayHello(@RequestParam String name) {
return demoService.sayHello(name);
}
}
5.在启动类上开启dubbo并扫描controller类(不加也可以,因为加了@RestController Spring会自动扫描相应的类,但有时不会加载 @Reference,最好写上)
package com.zc.dubboconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.zc.dubboconsumer.controller")
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
- 启动消费者,并访问 http://localhost:8080/sayHello?name=dubbo
image.png
成功调用到生产者的服务
五. 使用dubbo-admin监控服务
请查看这篇博文,就是个管理界面,搭起来然后把服务注册中心地址换成自己的就行了https://www.jianshu.com/p/3d619740883c
这是最终效果,生产者和消费者都能看到了
![12637001-eaf08772ee35efba.png](https://i-blog.csdnimg.cn/blog_migrate/2e41959bcc2fcde8e439c2a431abc3e8.png)
dubbo-admin.png