(二)Dubbo 分布式服务框架-快速入门

前言

Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,
只需用Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展进行加载。

Dubbo的常用方式有两种

  • 注解方式(常用)
  • XML方式(常用)
  • 属性配置方式
  • API配置方式

入门案例中我们就以注解方式进行讲解,后续会介绍XML方式的使用。

一、注册中心

Dubbo的注册中心可以使用很多种:

  • Multicast 注册中心
  • Zookeeper注册中心(官方推荐)
  • Redis注册中心
  • Simple注册中心

关于注册中心内容,可以后续详细介绍。在案例中我们使用Zookeeper注册中心的方式

1、Zookeeper 介绍

官方推荐使用 zookeeper 注册中心。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。
Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbox 服务的注册中心,工业强度较高,可用于生产环境。

2、Zookeeper 安装及启动

文章链接

二、代码编写

通用pom.xml文件(消费方端口需修改)


    4.0.0
    com.tcy
    dubbo-service
    0.0.1-SNAPSHOT
    war

    
        4.2.4.RELEASE
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        

        
        
            com.alibaba
            dubbo
            2.5.7
        
        
            org.apache.zookeeper
            zookeeper
            3.4.6
        
        
            com.github.sgroschupf
            zkclient
            0.1
        

        
            javassist
            javassist
            3.11.0.GA
        

    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.7
                    1.7
                
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                
                    
                    8080
                    
                    /
                
            
        
    



1、服务提供方
  • 创建服务方的接口及实现
package com.tcy.dubbo.service;

public interface HelloService {

    public String sayHello(String name);
}
package com.tcy.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.tcy.dubbo.service.HelloService;

/**
 * 交给dubbo的service注解处理
 * @author tong
 *
 */
@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        
        return "Hello"+name;
    }

}
  • 创建服务器方配置文件(applicationContext-service.xml)



    
    

    
    

    
    
    


  • web.xml


    dubbo-service
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    

    
    
        contextConfigLocation
        classpath:applicationContext*.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    


2、服务消费方
  • 创建消费方的Controller
package com.tcy.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.tcy.dubbo.service.HelloService;

@RestController
@RequestMapping("/hello")
public class HelloController {
    
    @Reference
    private HelloService helloService;
    
    @RequestMapping("/test")
    public String hello(){
        return helloService.sayHello("Jack");
    }

}

需加入接口、无需实现类


(二)Dubbo 分布式服务框架-快速入门_第1张图片
  • 创建消费放配置文件(springmvc.xml)


                        
                    
    
        
            
                
            
        
    

    
    

    
    

    
    
    


  • web.xml


    dubbo-consumer
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    
    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        SpringMVC
        *.action
    


注:引入Dubbo标签无提示或报错:解决方案如下

  • 下载dubbo.xsd文件
  • eclipse配置
    Eclipse --- Window --- Preferences


    (二)Dubbo 分布式服务框架-快速入门_第2张图片

注意:一定要记得手动写上/dubbo.xsd

三、启动服务测试

(二)Dubbo 分布式服务框架-快速入门_第3张图片

你可能感兴趣的:((二)Dubbo 分布式服务框架-快速入门)