前期准备:下载安装zookeeper作为服务注册中心
安装步骤
- 解压
tar zxvf zookeeper-3.4.8.tar.gz
- 将zoo_sample.cfg文件名修改为zoo.cfg,并修改配置文件中的内容。
默认为zoo.cfg,可以查看zookeeper-3.4.6\bin\zkEnv.cmd
dataDir=C:\\Users\\tianthe\\zk\\data
dataLogDir=C:\\Users\\tianthe\\zk\\log
修改数据存放的文件目录,并添加了存放日志文件的文件目录
- 启动zookeeper
cd zookeeper-3.3.6
cd bin
./zkServer.sh start
服务提供者
-
项目结构
依赖
<dependencies>
<!-- Spring Boot Dubbo -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 公共api:helloDubboService.java
public interface HelloDubboService {
String helloDubbo();
}
- 公共api的实现类
@Service
public class HelloDubboServiceImpl implements HelloDubboService {
@Override
public String helloDubbo() {
return "hello dubbo";
}
}
@Service注解导入的包为com.alibaba.dubbo.config.annotation.Service;
,通过这个注解将这个类申明为dubbo服务
- 配置文件:application.properties
server.port=8080
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.cqupt.dubbo.service
## 数据源配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.dubbo.application.name
:应用名称
spring.dubbo.registry.address
:注册地址
spring.dubbo.protocol.name
:协议名称
spring.dubbo.protocol.port
:协议端口
spring.dubbo.scan
:扫描的包
只有服务提供者配置文件中需要配置spring.dubbo.protocol.name
和spring.dubbo.protocol.port
,服务消费者不需要
服务调用者
-
项目结构
依赖
和服务提供者差不多公共api的实现类
@Service
public class HelloDubboServiceImpl implements HelloDubboService {
@Override
public String helloDubbo() {
return "hello dubbo";
}
}
- 控制类:HelloDubboController .java
@RestController
public class HelloDubboController {
@Reference
private HelloDubboService helloDubboService;
@RequestMapping("/hello")
public String helloDubbo() {
return helloDubboService.helloDubbo();
}
}
- 配置文件:application.properties
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.cqupt.dubbo.service
服务提供者和服务调用者公用一份api,一种是将公共api写到独立的项目中,并生成一个依赖。另外一种是每个项目中都写这个公共api,其中服务提供者需要实现公共api,而服务调用者只需写公共api即可
该项目的公共api为:helloDubboService
启动
1.启动zookeeper
2.启动SpringBoot-Dubbo-Provider
3.启动SpringBoot-Dubbo-Consumer
启动服务提供者和服务调用者时,只有都出现:
zookeeper state changed (SyncConnected)
[DUBBO] Register: consumer://192.168.1.103/com.cqupt.dubbo.service.HelloDubboService?application=consumer&category=consumers&check=false&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18968&side=consumer×tamp=1524742106566, dubbo version: 2.5.3, current host: 192.168.1.103
[DUBBO] Subscribe: consumer://192.168.1.103/com.cqupt.dubbo.service.HelloDubboService?application=consumer&category=providers,configurators,routers&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18968&side=consumer×tamp=1524742106566, dubbo version: 2.5.3, current host: 192.168.1.103
[DUBBO] Notify urls for subscribe url [dubbo://192.168.1.103:20880/com.cqupt.dubbo.service.HelloDubboService?anyhost=true&application=consumer&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18900&side=provider×tamp=1524742086150, empty://192.168.1.103/com.cqupt.dubbo.service.HelloDubboService?application=consumer&category=configurators&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18968&side=consumer×tamp=1524742106566, empty://192.168.1.103/com.cqupt.dubbo.service.HelloDubboService?application=consumer&category=routers&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18968&side=consumer×tamp=1524742106566], dubbo version: 2.5.3, current host: 192.168.1.103
[DUBBO] Successed connect to server /192.168.1.103:20880 from NettyClient 192.168.1.103 using dubbo version 2.5.3, channel is NettyChannel [channel=[id: 0x013e7344, /192.168.1.103:56955 => /192.168.1.103:20880]], dubbo version: 2.5.3, current host: 192.168.1.103
[DUBBO] Start NettyClient hetiantian/192.168.1.103 connect to the server /192.168.1.103:20880, dubbo version: 2.5.3, current host: 192.168.1.103
[DUBBO] Refer dubbo service com.cqupt.dubbo.service.HelloDubboService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=consumer&check=false&dubbo=2.5.3&interface=com.cqupt.dubbo.service.HelloDubboService&methods=helloDubbo&pid=18968&side=consumer×tamp=1524742106566, dubbo version: 2.5.3, current host: 192.168.1.103
说明dubbo服务启动成功并且已经注册到了zk中
程序运行结果:
可能会遇到的问题:
- 访问报错
java.lang.IllegalArgumentException: Invalid character found in method name.
HTTP method names must be tokens
https
请求-->http
请求
- log4j加载错误
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
... 35 common frames omitted
将启动父依赖的版本从2.0.1.RELEASE
-->1.5.1.RELEASE
- 启动报错
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_161]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_161]
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[zookeeper-3.4.6.jar:3.4.6-1569965]
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ~[zookeeper-3.4.6.jar:3.4.6-1569965]
zookeeper(zk)安装失败。皮皮甜昨天遇到这个问题以为是zookeeper版本和dubbo版本冲突,装了很多不同版本的zk,但是都没有解决。最后发现是zk安装失败了,皮皮甜安装失败的原因是配置文件中data和log的路径没有写正确
-
启动程序不报错,但是dubbo服务没有启动并且没有注册到zk中
公共api需要在相同的package下,即com.cqupt.dubbo.service
附:完整项目地址