springboot中整合dubbo2.6(dubbo+hessian双协议支持)
dubbo官网 :http://dubbo.apache.org/en-us/
dubbo文档: http://dubbo.apache.org/zh-cn/docs/user/new-features-in-a-glance.html
dubbo 目前的计划,维护两大版本:2.6.x和2.7.x 。
2.6.x 主要以 bugfix 和少量 enhancements 为主,因此能完全保证稳定性。
2.7.x 作为社区的主要开发版本,得到持续更新并增加了大量新 feature 和优化,同时也带来了一些稳定性挑战。
在实际项目开发中,我们通常都会采用最为稳定的框架,因此本篇介绍的是dubbo的2.6.x (当前最新版本2.6.9)版本。
准备创建三个java模块
dubboApi模块: 抽取公共接口、类元模型等 ,jar包,供dubboProducer和dubboConsumer模块引入。
dubboProducer模块 : dubbo服务提供者,一个springboot工程
dubboConsumer模块: dubbo服务消费者,一个springboot工程
完整代码如下
一、dubboApi模块
定义一个简单的java接口 UserService
package com.tingcream.dubboApi.user;
public interface UserService {
public String hello(String name);
}
二、dubboProducer模块
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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tingcream</groupId>
<artifactId>dubboProducer</artifactId>
<version>1.1</version>
<packaging>war</packaging>
<name>dubboProducer</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.tingcream</groupId>
<artifactId>dubboApi</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--内嵌tomcat支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- 引入jsp 、jstl支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- spring的织入 aspects cglib -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!--hutool 工具集合引入-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<!--dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.63</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>
<!--dubbo end -->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、application.yml配置
server:
port: 8081
servlet:
context-path: /
spring:
application:
name: dubboProducer
3、springboot启动类
package com.tingcream.dubboProducer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
@SpringBootApplication(scanBasePackages={"com.tingcream"})
@ImportResource("classpath:dubbo-producer.xml")
public class DubboProducerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DubboProducerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DubboProducerApplication.class);
}
@ResponseBody
@RequestMapping("/")
public String index(){
return "你好 DubboProducerApplication ";
}
}
4、dubbo-producer.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名称 -->
<dubbo:application name="dubbo-provider" />
<!-- 注册中心 -->
<dubbo:registry id="registry" address="zookeeper://192.168.11.10:2181" timeout="3000"/>
<!--暴露的服务使用的协议+端口 dubbo 20880 -->
<!--<dubbo:protocol id="dubbo" name="dubbo" port="20880" default="true" host="192.168.0.11" />
<dubbo:protocol id="hessian" name="hessian" port="14000" server="servlet" host="192.168.0.11" />
-->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="hessian" port="8088" server="jetty" />
<dubbo:consumer check="false" />
<!-- 声明一个spring的bean -->
<bean id="userService" class="com.tingcream.dubboProducer.user.UserServiceImpl"/>
<!-- 暴露一个service远程服务bean -->
<dubbo:service interface="com.tingcream.dubboApi.user.UserService" ref="userService"
timeout="300000" >
</dubbo:service>
</beans>
UserServiceImpl 业务类,实现UserService接口
package com.tingcream.dubboProducer.user;
import com.tingcream.dubboApi.user.UserService;
public class UserServiceImpl implements UserService {
@Override
public String hello(String name) {
return "hello: "+name;
}
}
二、dubboConsumer模块
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.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tingcream</groupId>
<artifactId>dubboProducer</artifactId>
<version>1.1</version>
<packaging>war</packaging>
<name>dubboProducer</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.tingcream</groupId>
<artifactId>dubboApi</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--内嵌tomcat支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- 引入jsp 、jstl支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- spring的织入 aspects cglib -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!--hutool 工具集合引入-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<!--dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.63</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>
<!--dubbo end -->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、application.yml配置
server:
port: 8082
servlet:
context-path: /
spring:
application:
name: dubboConsumer
3、springboot启动类
package com.tingcream.dubboConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
@SpringBootApplication(scanBasePackages = {"com.tingcream"})
@ImportResource("classpath:dubbo-consumer.xml")
public class DubboConsumerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DubboConsumerApplication.class);
}
@ResponseBody
@RequestMapping("/")
public String index(){
return "你好 DubboConsumerApplication ";
}
}
4、dubbo-consumer.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名称 -->
<dubbo:application name="dubbo-consumer" />
<!-- 注册中心 -->
<dubbo:registry id="registry" address="zookeeper://192.168.11.10:2181" timeout="3000"/>
<dubbo:consumer check="false" />
<!-- 生成远程服务代理,可以与本地bean一样使用 check属性,启动时候是否检查 一般设置成false 启动时候不检查 -->
<dubbo:reference id="userService" interface="com.tingcream.dubboApi.user.UserService" />
</beans>
UserController 类中注入dubboProducer提供的UserService接口,进行远程调用
package com.tingcream.dubboConsumer.user;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tingcream.dubboApi.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;//dubbo远程接口
@RequestMapping("/hello")
public String hello(String name){
String msg= userService.hello(name);
return msg;
}
}
启动dubboProducer和dubboConsumer服务器, 启动查看dubbo-admin管理后台
尝试访问dubboConsumer的/user/hello 接口, OK!!
附: dubbo-admin(2.6).zip 下载
链接:https://pan.baidu.com/s/1XPreaMjsngo2ATyiPkHXHw
提取码:92yr