SpringBoot-Dubbo

zookeeper安装

1
2
3
4
5
brew install zookeeper #安装
zkServer start #启动
zkcli -server localhost:2181 #客户端连接zk服务
[zk: localhost:2181(CONNECTED)] ls / # ls进行查看连接资源
[zk: localhost:2181(CONNECTED) 13] ls /dubbo/com.api.ITestService/providers #查看连接的提供者

springboot 引入dubbo

消费者和提供者公共配置

  1. 添加依赖

    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>
  2. 启用dubbo,在启动类添加注解@EnableDubbo

  3. 配置文件添加zk,spring.dubbo.registry.address = zookeeper://localhost:2181

  4. 添加接口

    1
    2
    3
    public interface ITestService {
    public String hello();
    }

提供者代码

1
2
3
4
5
6
7
8
9
import com.alibaba.dubbo.config.annotation.Service;

@Service
public class TestService implements ITestService {
@Override
public String hello() {
return "hello world!";
}
}

消费者代码

1
2
3
4
5
6
7
8
9
10
11
12
import com.alibaba.dubbo.config.annotation.Reference;

@RestController
public class TestController {

@Reference(check = false)
ITestService iTestService;

@GetMapping("hello")
public String hello(){
return iTestService.hello();
}

常见问题

  1. 异常信息

    1
    2
    cause: 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>
  2. 异常信息

    1
    2
    java.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

  3. 异常信息

    1
    2
    failed 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

  4. 异常信息

    1
    2
    3
    c.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拦截了