最近在项目开发中,用到了ZooKeeper + Dubbo的微服务框架,项目是Maven Module Project结构。我不想做过多的文字叙述,网络资源很多,主要是讲述下我是怎么搭建的。
首先我们要下载zookeeper,我用到的版本是zookeeper-3.4.10,目录如下:
进入到conf目录,我们需要copy一份zoo_sample.cfg文件,修改文件名为zoo.cfg,里面内容为:
然后我们再进入bin目录如下:
我用的是window本,等项目配置好之后,我们首先要启动ZooKeeper,执行文件zkServer.cmd。
现在来说下maven项目,我maven项目用到的是我自己的两个工程,Maven Module 大家自己去搭建,网上资源很多。先展示下我的两个项目:
生产者项目: 消费者项目:
首先在两个项目中的pom.xml文件中添加如下依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
然后看下生产者的service代码:
package com.framework.pt.service;
public interface DubboService {
public String testDubboMsg(String s);
}
package com.framework.pt.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.framework.pt.service.DubboService;
@Service
public class DubboServiceImpl implements DubboService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public String testDubboMsg(String s) {
logger.info("----------调用成功----------");
return s;
}
}
然后看下applicationContext-dubbo.xml文件内容:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" >
<bean id ="dubboService" class="com.framework.pt.service.impl.DubboServiceImpl" />
<dubbo:application name="dubbo_prividor" logger="slf4j" />
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="false" subscribe="false"/>
<!-- <dubbo:protocol accesslog="true" name="dubbo" port="20880" /> -->
<dubbo:service interface="com.framework.pt.service.DubboService" ref="dubboService"/>
</beans>
关于spring框架的搭建我的其他文章多次提到,这里不再详细叙述,这样我们的生产者项目就搭建完毕了。
接着我们看下消费者项目的部分代码内容:
service接口其实和生产者项目是一样的,包路径保持一致
package com.framework.pt.service;
public interface DubboService {
public String testDubboMsg(String s);
}
我写了一个controller来前台调用service,代码如下:
package com.framework.fund.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.framework.fund.service.HelloWorldService;
import com.framework.pt.service.DubboService;
@Controller
@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
public class HelloWorldController {
@Autowired
private HelloWorldService helloWorldService;
@Autowired
private DubboService dubboService;
@RequestMapping(value ="/print",produces="text/html;charset=UTF-8")
@ResponseBody
public String print(HttpServletRequest request, HttpServletResponse response) {
return dubboService.testDubboMsg("测试Dubbo服务");
}
}
现在看下消费者的applicationContext-dubbo.xml文件内容:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" >
<dubbo:application name="dubbo_consumer" logger="slf4j" />
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="false"/>
<dubbo:reference interface="com.framework.pt.service.DubboService" id="dubboService" />
</beans>
我们的项目工程配置好了,现在开始启动服务,首先先启动ZooKeeper,执行刚才的文件,启动后的界面如下:
然后启动生产者,项目用jetty启动的,启动完如下:
再次启动消费者项目工程,也是jetty起的,启动界面如下:
我们现在就开始用浏览器调用接口,界面如下:
我们的项目搭建成功了,我们只是简单的搭建,实战用还会用到其他的技术,我们慢慢的摸索,一起加油努力~