听说微服务很火,貌似我也正在用,给有需要的同学介绍一下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
- 运行E:\software\tools\zookeeper-3.4.9\conf\bin目录下的zkServer.cmd文件,显示如下信息则配置成功。
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,得到如下界面则表示消费者成功访问服务端。
关于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>
源码地址:https://github.com/MAXAmbitious/lvba
这个demo是我之前搭建用于一个项目的枪版,有问题欢迎一起探讨!