开始接触spring boot2整合dubbo时,参考了好多文章,都不太完全,导致整合屡次失败。坚信降低门槛才会普及大众,否则技术再好束之高阁也白搭。
dubbo配置文件分好几种:xml类、properties类、api配置类、注解类。另外注册中心常用zookeeper和redis等。spring boot和dubbo版本不同。种种组合稍有出入就会导致失败,给初学者造成非常大困扰~
遇到的四处大坑和经验:
1、在注册中心上栽过坑,后来采用zookeeper作为中心解决。曾以redis试了好久没成功,改为zookeeper立马成功,看来dubbo推荐zookeeper是有道理的。(--追加说明,redis注册中心也配置好了,漏了用户和密码配置。使用jedis 2.9.0版本成功,2.9.1版本启动报错)
(正确的redies注册中心配置:<dubbo:registry address="redis://localhost:6379" username="root" password="123456"/>)
2、暴露dubbo服务上也栽过坑,在provider实现的类以@Service暴露成功(选的是阿里的import com.alibaba.dubbo.config.annotation.Service;)
3、加载xml文件也栽过坑,后来找到方法,在spring boot启动中加载@ImportResource({"classpath:dubbo/consumer.xml"}) 解决。
@SpringBootApplication
@ImportResource({"classpath:dubbo/consumer.xml"})
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
4、dubbo程序启动进行注册有时会报错,可以忽略,如下
2019-01-20 16:58:14.853 INFO 1036 --- [ main] c.a.d.r.zookeeper.ZookeeperRegistry : [DUBBO] Register: dubbo://192.168.0.106:20880/cc.itil.oms.api.DemoService?anyhost=true&application=myService&dubbo=2.6.2&generic=false&interface=cc.itil.oms.api.DemoService&methods=sayHello&pid=1036&side=provider&timeout=600000×tamp=1547974675270, dubbo version: 2.6.2, current host: 192.168.0.106
2019-01-20 16:58:19.923 ERROR 1036 --- [ main] org.apache.curator.ConnectionState : Connection timed out for connection string (localhost:2181) and timeout (5000) / elapsed (22147)
org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss
at org.apache.curator.ConnectionState.checkTimeouts(ConnectionState.java:225) [curator-client-2.12.0.jar:na]
at org.apache.curator.ConnectionState.getZooKeeper(ConnectionState.java:94) [curator-client-2.12.0.jar:na]
at org.apache.curator.CuratorZookeeperClient.getZooKeeper(CuratorZookeeperClient.java:117) [curator-client-2.12.0.jar:na]
at org.apache.curator.framework.imps.CuratorFrameworkImpl.getZooKeeper(CuratorFrameworkImpl.java:489) [curator-framework-2.12.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$3.call(ExistsBuilderImpl.java:237) [curator-framework-2.12.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$3.call(ExistsBuilderImpl.java:226) [curator-framework-2.12.0.jar:na]
##############华丽丽的分割线##########
好了,接下来把全部文件粘贴出来,供大家参考,也加深自己的印象。
环境说明
1、Eclipse Jee Oxygen(win10)
2、zookeeper(zookeeper-3.4.13/centos7)
3、spring-boot-starter-parent(2.1.2.RELEASE)
4、jdk 1.8
5、dubbo-spring-boot-starter(0.2.0)
6、分两个项目(dubbo-provider和dubbo-consumer),两个项目中都存在接口类DemoService
相关截图如下:
1、eclipse目录
2、dubbo-provider项目相关配置文件(配置文件路径请参看以上eclipse展图)。
2-1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cc.itil</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot2.0集成redis所需添加jedis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--springboot2.0集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2-2、resources/application.properties文件为空。
2-3、resources/dubbo/provider.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cc.itil.oms.api.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="cc.itil.oms.api.impl.DemoServiceImpl" />
3、dubbo-provider项目相关java文件
3-1、DemoService.java
package cc.itil.oms.api;
public interface DemoService {
String sayHello(String name);
}
3-2、DemoServiceImpl.java
package cc.itil.oms.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import cc.itil.oms.api.DemoService;
@Service
public class DemoServiceImpl implements DemoService{
public String sayHello(String name) {
System.out.println("Hello " + name);
return "Hello provider" + name;
}
}
3-3、DubboProviderApplication.java
package cc.itil.oms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:dubbo/provider.xml"})
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
3-4、Provider.java(没啥用,留不留都行)
package cc.itil.oms;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Provider {
@RequestMapping(value="/")
String home() {
System.out.println("Hello World!");
return "Hello World!";
}
}
4、dubbo-consumer项目相关配置文件(配置文件路径请参看以上eclipse展图)。
4-1、pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cc.itil</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot2.0集成redis所需添加jedis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!--springboot2.0集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4-2、resources/application.properties文件修改端口。
server.port = 9090
4-3、resources/dubbo/consumer.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="cc.itil.oms.api.DemoService" />
</beans>
5、dubbo-consumer项目相关java文件
5-1、DemoService.java
package cc.itil.oms.api;
public interface DemoService {
String sayHello(String name);
}
5-2、DubboConsumerApplication.java
package cc.itil.oms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource({"classpath:dubbo/consumer.xml"})
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
5-3、DemoConsumerController.java
package cc.itil.oms;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import cc.itil.oms.api.DemoService;
@RestController
public class DemoConsumerController {
@Reference
private DemoService demoService;
@RequestMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name){
return demoService.sayHello(name);
}
}
6、结果验证
在浏览器输入http://localhost:9090/sayHello/qiaosl
eclipse日志显示如下
7、祝你好运~