1. 程式人生 > >windows環境dubbo搭建手冊

windows環境dubbo搭建手冊

聽說微服務很火,貌似我也正在用,給有需要的同學介紹一下dubbo的搭建吧。

首先宣告這篇文章將介紹以下模組:
1. zookeeper搭建
2. dubbo+springmvc+mybatis demo(主要介紹dubbo)

原始碼地址會在本文最後提供,可直接執行!

zookeeper搭建

http://www.apache.org/dist//zookeeper/stable/zookeeper-3.4.7.tar.gz下載壓縮包,解壓到自己想要安裝的目錄即可(不需要安裝)。
1. 進入安裝目錄E:\software\tools\zookeeper-3.4.9\conf,將zoo_sample.cfg檔案拷貝一份並命名為zoo.cfg
2. 將zoo.cfg檔案內容修改成:(主要改資料和日誌的本地儲存目錄)

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes. dataDir=E:\\zookeeper-3.4.7\\data dataLogDir=E:\\zookeeper-3.4.7\\log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the
# administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
  1. 執行E:\software\tools\zookeeper-3.4.9\conf\bin目錄下的zkServer.cmd檔案,顯示如下資訊則配置成功。
    image

dubbo+springmvc+mybatis

生產者程式碼配置

服務生產者類UserFacade

package com.lvba.customer.user.facade;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lvba.customer.user.api.IUserFacade;
import com.lvba.customer.user.dto.UserDTO;
import com.lvba.customer.user.service.inter.IUserService;

@Component
public class UserFacade implements IUserFacade{

    @Autowired
    IUserService userService;

    public UserDTO geUserDTOByKey(Long id) {
        return userService.getUserDTOByKey(id);
    }

}

用spring配置宣告暴露服務

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
       http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

     <import resource="classpath:/META-INF/spring/application.xml" />

     <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/> 

     <!--使用 zookeeper 註冊中心暴露服務,注意要先開啟 zookeeper-->
     <dubbo:registry address="zookeeper://localhost:2181"/>   
     <!-- 用dubbo協議在20880埠暴露服務 -->
     <dubbo:protocol name="dubbo" port="20880" />

     <context:component-scan base-package="com.lvba.customer;">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
     </context:component-scan>

     <dubbo:service ref="userFacade" interface="com.lvba.customer.user.api.IUserFacade" version="1.0"  />

</beans>

服務啟動類LanchProvider

package com.lvba.customer.dubbo.launch;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LanchProvider {

     public static void main(String[] args) throws IOException {
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/*.xml");
         context.start();
         System.out.println("服務已經啟動...");
         System.in.read();
     }
}

消費者程式碼配置

服務介面IUserFacade

package com.lvba.customer.user.api;

import com.lvba.customer.user.dto.UserDTO;

public interface IUserFacade {

    public UserDTO geUserDTOByKey(Long id);
}

Spring配置引用遠端服務

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

     <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/> 

     <!--使用 zookeeper 註冊中心暴露服務,注意要先開啟 zookeeper-->
     <dubbo:registry address="zookeeper://localhost:2181"/>   

     <dubbo:reference interface="com.lvba.customer.user.api.IUserFacade" version="1.0" id="userFacade" />

</beans>

服務呼叫方UserController

package com.lvba.customer.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lvba.customer.user.api.IUserFacade;
import com.lvba.customer.user.dto.UserDTO;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserFacade userFacade;

    /**
     * @return
     */
    @RequestMapping("/getUser")
    public ModelAndView getUser(){
        ModelAndView mv = new ModelAndView("user");
        UserDTO userDTO = userFacade.geUserDTOByKey(1L);
        mv.addObject("user", userDTO);
        mv.addObject("cc", "xxxx");
        return mv;
    }
}

驗證

Java啟動LanchProvider類,出現如下提示則表明服務註冊成功:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
服務已經啟動...

jetty(或tomcat)啟動lvba-web工程,在瀏覽器上訪問http://localhost:8091/user/getUser,得到如下介面則表示消費者成功訪問服務端。
image

關於maven引入dubbo時要注意spring版本相容問題

 <dependency>
        <groupId>com.alibaba</groupId> 
        <artifactId>dubbo</artifactId>
        <version>2.5.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
        </exclusions>
    </dependency>