springboot中整合dubbo2.6(dubbo+hessian双协议支持)

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

springboot中整合dubbo2.6(dubbo+hessian双协议支持)_第1张图片

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工程

springboot中整合dubbo2.6(dubbo+hessian双协议支持)_第2张图片

 

完整代码如下

一、dubboApi模块

定义一个简单的java接口  UserService

package com.tingcream.dubboApi.user;
 
public interface UserService {
 
    public String hello(String name);
}

 

二、dubboProducer模块

1、pom.xml配置



  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.7.RELEASE
     
  
  com.tingcream
  dubboProducer
  1.1
  war
  dubboProducer
  Demo project for Spring Boot
 
  
 
    
      com.tingcream
      dubboApi
      1.1
    
 
    
      org.springframework.boot
      spring-boot-starter-web
    
 
 
    
      org.projectlombok
      lombok
      true
    
 
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
 
 
    
    
      org.springframework.boot
      spring-boot-starter-tomcat
      provided
      true
    
    
    
      org.apache.tomcat.embed
      tomcat-embed-jasper
      provided
    
    
      javax.servlet
      jstl
    
 
 
 
    
    
      org.springframework
      spring-aspects
    
    
      cglib
      cglib-nodep
      3.2.10
    
 
 
    
      com.alibaba
      fastjson
      1.2.31
    
 
 
    
    
      cn.hutool
      hutool-all
      5.1.0
    
 
 
    
    
      com.alibaba
      dubbo
      2.6.9
    
 
    
      com.101tec
      zkclient
      0.11
    
 
    
      org.apache.curator
      curator-framework
      4.3.0
    
 
    
      io.netty
      netty-all
      4.1.50.Final
    
 
    
      com.caucho
      hessian
      4.0.63
    
 
    
      org.mortbay.jetty
      jetty
      6.1.26
    
 
    
 
 
 
  
 
  
    
      
        src/main/java
        
          **/*
        
        
          **/*.java
        
      
      
        src/main/resources
        
          **/*
        
        
          **/*.java
        
      
    
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
 
 
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.8
          1.8
          utf-8
        
      
    
 
  
 

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配置



 
  
  
 
  
  
 
  
  
   
   
 
  
 
  
  
 
  
  
  
 

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配置



  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.7.RELEASE
     
  
  com.tingcream
  dubboProducer
  1.1
  war
  dubboProducer
  Demo project for Spring Boot
 
 
 
  
 
    
      com.tingcream
      dubboApi
      1.1
    
 
    
      org.springframework.boot
      spring-boot-starter-web
    
 
 
    
      org.projectlombok
      lombok
      true
    
 
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
 
 
    
    
      org.springframework.boot
      spring-boot-starter-tomcat
      provided
      true
    
    
    
      org.apache.tomcat.embed
      tomcat-embed-jasper
      provided
    
    
      javax.servlet
      jstl
    
 
 
 
    
    
      org.springframework
      spring-aspects
    
    
      cglib
      cglib-nodep
      3.2.10
    
 
 
    
      com.alibaba
      fastjson
      1.2.31
    
 
 
    
    
      cn.hutool
      hutool-all
      5.1.0
    
 
 
    
    
      com.alibaba
      dubbo
      2.6.9
    
 
    
      com.101tec
      zkclient
      0.11
    
 
    
      org.apache.curator
      curator-framework
      4.3.0
    
 
    
      io.netty
      netty-all
      4.1.50.Final
    
 
    
      com.caucho
      hessian
      4.0.63
    
 
    
      org.mortbay.jetty
      jetty
      6.1.26
    
 
    
 
 
 
  
 
  
    
      
        src/main/java
        
          **/*
        
        
          **/*.java
        
      
      
        src/main/resources
        
          **/*
        
        
          **/*.java
        
      
    
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
 
 
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.8
          1.8
          utf-8
        
      
    
 
  
 

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配置



 
  
  
 
  
  
 
  
 
  
  
 

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管理后台

springboot中整合dubbo2.6(dubbo+hessian双协议支持)_第3张图片

尝试访问dubboConsumer的/user/hello 接口, OK!!

springboot中整合dubbo2.6(dubbo+hessian双协议支持)_第4张图片

附:   dubbo-admin(2.6).zip 下载

链接:https://pan.baidu.com/s/1XPreaMjsngo2ATyiPkHXHw 
提取码:92yr 

你可能感兴趣的:(java编程,springboot,dubbo,springboot,alibaba,hessian)