SpringBoot-Dubbo
zookeeper安装
1 | brew install zookeeper #安装 |
springboot 引入dubbo
消费者和提供者公共配置
添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.33.Final</version>
</dependency>启用dubbo,在启动类添加注解
@EnableDubbo
配置文件添加zk,
spring.dubbo.registry.address = zookeeper://localhost:2181
添加接口
1
2
3public interface ITestService {
public String hello();
}
提供者代码
1 | import com.alibaba.dubbo.config.annotation.Service; |
消费者代码
1 | import com.alibaba.dubbo.config.annotation.Reference; |
常见问题
异常信息
1
2cause: io/netty/bootstrap/ServerBootstrap
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:287)[dubbo-2.6.6.jar:2.6.6]解决:添加netty依赖
1
2
3
4
5<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.33.Final</version>
</dependency>异常信息
1
2java.lang.IllegalArgumentException: Specified invalid port from env value:0
at com.alibaba.dubbo.config.ServiceConfig.parsePort(ServiceConfig.java:691) ~[dubbo-2.6.6.jar:2.6.6]解决:环境变量设置有问题,在vm上覆写参数
-DDUBBO_PORT_TO_BIND=20880
异常信息
1
2failed to connect to server /10.0.0.54:20880, error message is:connection timed out: /10.0.0.54:20880
at com.alibaba.dubbo.remoting.transport.netty4.NettyClient.doConnect(NettyClient.java:127) ~[dubbo-2.6.6.jar:2.6.6]解决:多网卡下ip绑定错误,导致连接超时,在vm添加参数
-DDUBBO_IP_TO_BIND=10.9.98.133
绑定ip异常信息
1
2
3c.c.b.s.w.a.GlobalExceptionHandler : application error
com.alibaba.dubbo.rpc.RpcException: No provider available from registry 192.168.240.15:2181 for service cn.com.api.dubbo.service.TestDubboService:1.0.0 on consumer 10.233.73.69 use dubbo version 2.6.6, please check status of providers(disabled, not registered or in blacklist).
at com.alibaba.dubbo.registry.integration.RegistryDirectory.doList(RegistryDirectory.java:590)解决:如果zookeeper都显示提供者和消费者都注册通知正常,但是消费者缓存里面拿不到提供者的服务地址,检查
spring.dubbo.invoke.invalid.ips
这个配置是否把你的ip拦截了