springboot-19-整合dubbox
springboot 整合dubbox
1, 没了,,,
2, 安装zookeeper
可见: http://www.cnblogs.com/wenbronk/p/6636926.html
2.1 下载:
wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.8/
解压:
tar -zxf zookeeper-3.4.8.tar.gz
修改配置文件:
cd zookeeper-3.4.8/conf cp zoo_sample.cfg zoo.cfg # zookeeper 默认是用../conf/zoo.cfg 如果没有这个文件则报错 vim zoo.cfg
其他, 能启动就行吧
cd zookeeper-3.4.8/bin ./start.sh start #启动zookeeper 关闭: ./start.sh stop
我的zookeeper是安装在docker上的, 安装方式可见: http://www.cnblogs.com/wenbronk/p/6438492.html
3, maven工程
|--dubbo-prent 父工程(pom) | |_____dubbo-cosumer 消费者(war) | |_____duboo-provide 生产者(pom) |
|_____interface 消费者和生产者都依赖这个, 一个接口, 实现rpc(jar)
|
|_____service 实际的rpc调用的方法(war)
其中interface 被 dubbo-consumer和service所依赖
4, parent
parent主要用来统一管理使用的, 在其pom中进行jar包的统一依赖管理
pom.xml配置:
<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> <groupId>com.wenbronk</groupId> <artifactId>dubbo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 添加父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <spring-boot.version>1.5.2.RELEASE</spring-boot.version> <java.version>1.8</java.version> <fastjson.version>1.2.21</fastjson.version> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
5, dubbo-provide, 这儿没什么用, 管理生产者和接口, 实际中还可以用来依赖统一的工具包等
pom.xml中没啥东西
<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>com.wenbronk</groupId> <artifactId>dubbo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-provide</artifactId> <packaging>pom</packaging> <modules> <module>dubbo-provide-service</module> <module>dubbo-provide-interface</module> </modules> </project>
6, interface:
用来rpc调用的接口, 只有一个类:
TestService.java
package com.wenbronk.dubbo.service; public interface TestService { public String test(); }
7, service
服务的生产者:
pom.xml中用来依赖包
<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>com.wenbronk</groupId> <artifactId>dubbo-provide</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo-provide-service</artifactId> <packaging>war</packaging> <dependencies> <!-- exclude掉spring-boot的默认log配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- spring-boot的web启动的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 为了构建一个即是可执行的,又能部署到一个外部容器的war文件,你需要标记内嵌容器依赖为"provided" --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.wenbronk</groupId> <artifactId>dubbo-provide-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <!-- main方法运行需要 --> <!-- 加入热部署插件, spring-boot:run可用 --> <!-- java可用, 需要下载jar包放在lib下, 然后修改vm参数 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <!--springloaded hot deploy --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
发布dubbo服务, 在application.properties中加入以下配置:
server.port=8081 # Dubbo 消费者 spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://192.168.50.202:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.wenbronk.dubbo
注意配置 scan的扫描路径
App.java
package com.wenbronk.dubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } }
TestServiceImpl.java
package com.wenbronk.dubbo.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.wenbronk.dubbo.service.TestService; @Service(version="1.0.0") public class TestServiceImpl implements TestService { @Override public String test() { System.out.println("success"); return "finally, i am coming"; } }
8, 最后, 是cosumer
pom.xml中配置, 同service中一样,
application.properties
## 避免和 server 工程端口冲突 server.port=8080 ## Dubbo 服务消费者配置 spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://192.168.50.202:2181 spring.dubbo.scan=com.wenbronk.dubbo
App.java 同service一样
TestController.java
package com.wenbronk.dubbo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.wenbronk.dubbo.service.TestService; @RestController public class TestController { @Reference(version="1.0.0") private TestService testService; @RequestMapping("/abc") public String test() { System.out.println("controller.sucess"); testService.test(); return "success"; } }
最初因为说dubbo-start不支持新的spring了, 所以下载了当当的dubbox自己编译, 后来发现, 还是可以用的
附: dubbox编译
dubbo, 阿里不维护了, 当当继续开源: 因为没有发布在仓库, 所以自己编译
git clone https://git.oschina.net/smarti/spring-boot-mybatis-sample.git
然后跳过test进行编译, 可以发布在本地仓库中
mvn install -Dmaven.test.skip=true
听说用注解的话, 不支持事物, 还没遇到, 遇到在更新