dubbo的简单示例

构建过程

dubbo服务有服务方provider和消费方consumer,整个项目用tomcat做容器,在创建demo之前请先安装zookeeper
项目结构如图


image.png

image.png

image.png

主pom.xml



    4.0.0

    com.haijunyin
    dubbodemo-simple-parent
    pom
    1.0-SNAPSHOT
    
        dubbodemo-simple-provider
        dubbodemo-simple-consumer
        dubbodemo-simple-interface
    


provider

pom.xml



    
        dubbodemo-simple-parent
        com.haijunyin
        1.0-SNAPSHOT
    
    4.0.0

    dubbodemo-simple-provider

    war

    
        
        
            com.haijunyin
            dubbodemo-simple-interface
            1.0-SNAPSHOT
        

        
        
            org.springframework
            spring-web
            4.3.8.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.3.8.RELEASE
        

        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
        
            com.alibaba
            dubbo
            2.5.5
        

        
        
            com.github.sgroschupf
            zkclient
            0.1
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.6
                    1.6
                
            
        
    


web.xml



    web02

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        springmvc
        /
    


springmvc-servlet.xml




    


dubbo-provider.xml




    
    

    
    
    
    
    
    
    
    


HelloServiceImpl.java

package com.haijunyin.dubbodemo.simple.provider.services.impl;

import com.haijunyin.dubbodemo.simple.services.HelloService;

public class HelloServiceImpl implements HelloService {

    @Override
    public String hello(String name) {
        System.out.println("服务端收到服务:...hello...");
        return "你好:" + name;
    }

}

consumer

pom.xml



    
        dubbodemo-simple-parent
        com.haijunyin
        1.0-SNAPSHOT
    
    4.0.0

    dubbodemo-simple-consumer
    war

    
        
        
            com.haijunyin
            dubbodemo-simple-interface
            1.0-SNAPSHOT
        

        
        
            org.springframework
            spring-web
            4.3.8.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.3.8.RELEASE
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            com.alibaba
            dubbo
            2.5.5
        

        
            com.github.sgroschupf
            zkclient
            0.1
        
    

web.xml



    web02

    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        springmvc
        /
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    


springmvc-servlet.xml




    
    

    
    

    
    

    
    

    
    

    
    
        
        
        
        
    

dubbo-consumer.xml




    
    
    
    
    
    
    
    

HelloController.java

package com.haijunyin.dubbodemo.simple.consumer.controller;

import com.haijunyin.dubbodemo.simple.services.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "hello")
    public void hello(@RequestParam(defaultValue = "name") String name, HttpServletResponse resp) throws IOException {
        String message = helloService.hello(name);
        //输出,解决浏览器乱码问题
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(message);
    }

}

interface

pom.xml



    
        dubbodemo-simple-parent
        com.haijunyin
        1.0-SNAPSHOT
    
    4.0.0

    dubbodemo-simple-interface



HelloService.java

package com.haijunyin.dubbodemo.simple.services;

public interface HelloService {

    String hello(String name);

}

运行结果

首先启动zookeeper,然后配置启动provider的tomcat,然后配置启动consumer的tomcat,在浏览器中输入

http://localhost:8072/hello?name=小明

返回结果

你好:小明

说明demo成功

你可能感兴趣的:(dubbo的简单示例)