因为项目需求,所以需要用到远程的dubbo和zookeeper,之前没有接触过分布式,查了很多博客,代码copy过来各种报错,后来慢慢找了好久,总结了一个简单的demo,如果有不对的地方欢迎指正!感谢感谢感谢~
** 因为我的的项目是连接远程的zookeeper,所以我并没有在我本地配置,如果有需求,看一下这个链接
点我点我点我
接下来,开始我的表演
1. 生产者
① 先上一个生产者项目的目录
② pom.xml文件
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
③ application.yml文件
#dubbo配置
dubbo:
application: # 应用配置,配置当前应用信息,不管当前是提供者还是消费者.
name: Provide # 注册在注册中心的名称,唯一标识
registry: # 注册中心配置,用于配置连接注册中心相关信息
address: zookeeper://192.168.1.87:2181
protocol: # 暴露服务端口
# 默认名称,勿更改name: dubbo
port: 12398 # 暴露服务端口(默认为20880,修改端口,不同的服务提供者端口不能重复)
scan:
base-packages: com.dubboService # 生产者的包
scan:base-packages: 该配置中写生产者暴露服务的包
protocal下的name,默认为dubbo,如果生产者内随意更改这个name值导致dubbo报错,如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:36:41.705 ERROR org.springframework.boot.SpringApplication 858 reportFailure - Application run failed java.lang.IllegalStateException: No such extension com.alibaba.dubbo.rpc.Protocol by name dobbo
at com.alibaba.dubbo.common.extension.ExtensionLoader.findException(ExtensionLoader.java:482) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.common.extension.ExtensionLoader.createExtension(ExtensionLoader.java:489) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.common.extension.ExtensionLoader.getExtension(ExtensionLoader.java:309) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.findConfigedPorts(ServiceConfig.java:641) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:471) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:216) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:123) ~[dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:49) ~[dubbo-2.6.2.jar:2.6.2]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at com.miyu.mfmsserver.MfmsServerApplication.main(MfmsServerApplication.java:11) [classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.RELEASE.jar:2.1.0.RELEASE]
④ 来一个超级简单的Service
package com.dubboService;
public interface ComputeService {
Integer add(int a, int b);
}
⑤来一个实现类
package com.dubboService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service
@Component
public class ComputeServiceImpl implements ComputeService {
@Override
public Integer add(int a, int b) {
return a + b;
}
}
注意@Service这个注解是 com.alibaba.dubbo.config.annotation.Service下的,不要引错了!!!
生产者配置完毕,可以启动一下项目,日志中出现以下,证明我们的dubbo配置文件是没有问题的:
16:40:24.212 INFO com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener 68 onApplicationEvent - Dubbo Config was overridden by externalized configuration {dubbo.application.name=Provide, dubbo.protocol.port=12398, dubbo.registry.address=zookeeper://192.168.1.87:2181, dubbo.scan.base-packages=com.dubboService}
连接成功的话,日志中出现以下,证明我们连接成功:
2. 消费者
① 来一个目录
因为有公司名,包名部分我打了马赛克hhhh,一定要注意!生产者和消费者的Service包名一定要相同!不然找不到!
② pom.xml文件
同生产者,一毛一样的,copy来即可…
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 以下的部分!! -->
<!-- SpringBoot 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>0.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
③ yml配置文件
#dubbo配置
dubbo:
application: # 应用配置,用于配置当前应用信息,不管当前应用是提供者还是消费者.
name: Consumer # 注册在注册中心的名称,唯一标识
registry: # 注册中心配置,用于配置连接注册中心相关信息
address: zookeeper://192.168.1.87:2181
protocol: # 暴露服务端口
name: dubbi # 默认名称,勿更改
port: 12398 # 暴露服务端口(默认为20880,修改端口,不同的服务提供者端口不能重复)
server:
port: 8081
关于protocol的配置:在消费者中,name可以随便更改,port也不必与生产者一致,改完之后,我的项目依旧可以获取到生产者的service,应该是可以不配置的,如果后期查到相关的我会再更改
④ 建一个相同的包,写一个和生产中的service相同 的service
package com.dubboService;
public interface ComputeService {
Integer add(int a, int b);
}
⑤ 来一个测试controller
package com.debbotest.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubboService.ComputeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class dubboTestController {
@Reference
private ComputeService computeService;
@GetMapping("/test")
public void test(){
Integer result = computeService.add(1,3);
System.out.println("#####################################"+result+"###################################");
}
}
3. 然后我们开始测试
控制台输出结果:
完成!!!