jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建

为了实”微服”,用了dubbo管理各个生产者,消费者之间的关系,使服务之间透明。主要关系:
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第1张图片
图凑活看吧,个人理解大概意思是这样子。
注解:
zookeeper负责rpc服务之间的通信,
dubbo负责系统之间的服务的管理(github下载连接),
consumer服务消费者,消费者一般是是web工程(负责接收htttp等请求,可以理解为带有controller层),
provider为生产者,一般是业务,为service层,
所以上面流程对应途中的顺序是
1,dubbo注册到zookeeper为通信做准备,
2,服务生产者暴露服务接口到dubbo,
3,服务消费者注册需要的服务到dubbo,
4,消费者通过获取的到的对应的服务接口调用对应的servcie接口,
环境搭建:
下载和系统对应的jdk,zookeeper,dubbo-admin(我能提供2.5.4版本的);
jdk安装不在说,

一,zookeeper安装win7直接点击.exe按照提示就行,mac安装可以用:

$brew install zookeeper
==> Downloading https://homebrew.bintray.com/bottles/zookeeper-3.4.6_1.mavericks.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring zookeeper-3.4.6_1.mavericks.bottle.2.tar.gz
==> Caveats
To have launchd start zookeeper at login:
  ln -sfv /usr/local/opt/zookeeper/*.plist ~/Library/LaunchAgents
Then to load zookeeper now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.zookeeper.plist
Or, if you don't want/need launchctl, you can just run:
  zkServer start
==> Summary
��  /usr/local/Cellar/zookeeper/3.4.6_1: 207 files, 13M

安装完成可以从下面目录看下配置文件(win环境下zoo.cfg是没有安装之后,所以可以把zoo_sample.cfg复制从新命名为zoo.cfg,原因是zk启动的时候需要加载)。

ls /usr/local/etc/zookeeper
defaults        log4j.properties    zoo.cfg         zoo_sample.cfg

zookeeper服务启动

$ zkServer  start
JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED

可以用zk客户端看是否可连接:

$zkCli
Connecting to localhost:2181

Welcome to ZooKeeper!
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 
[zk: localhost:2181(CONNECTING) 0] 
WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0] ls
[zk: localhost:2181(CONNECTED) 1] ls /

二,dubbo-admin安装:
打开tomcat找到webapps删除默认的ROOT把下载好的复制到这目录,找到WEB—INF文件,可以看到有个dubbo.properties文件,打开之后看到

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

第一行识需要注册到的zookeeper服务地址,填写zookeeper安装的ip替换127.0.0.1,如果在本地(zk和dubbo在一台机器上)安装就不用替换了,下面的用户名和密码,可以自己修改。
三,启动tomcat;访问localhost:8080会提示输入密码,输入上面的用户密码就可以看到画面,证明dubbo-admin安装注册成功:
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第2张图片
四,生产者和消费者项目创建,
可以为maven或者一般的普通项目,只要能把需要的jar导入就行,
除了spring和springmvc需要jar之外,几个重要jar分别是

//dubbo--jar,
<dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
            <exclusions>
                <exclusion>
                //为了解决和spring-jar的冲突
             <groupId>org.springframeworkgroupId>
                    <artifactId>springartifactId>
                exclusion>
            exclusions>
        dependency>
        //zookeeper--jar
         <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.6version>
        dependency>
       //zookeeper客户端jar,
      <dependency>
         <groupId>com.github.sgroschupfgroupId>
         <artifactId>zkclientartifactId>
         <version>0.1version>
      dependency>

生产项目:
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第3张图片
看到没有controller,当然这里我把dao也没写呢,重点在servcie和servcieImpl,
配置文件在spring文件中配置:


<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:mvc="http://www.springframework.org/schema/mvc"  xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <mvc:annotation-driven />
    
    <context:component-scan base-package="com.java.spring" />
      
   <dubbo:application name="dubbo_provider">dubbo:application>
     
  <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register="true">dubbo:registry>

  
  <dubbo:protocol accesslog="true" name="dubbo" port="20880" />
    //注:如果要做负载均衡就要更改20880这个端口,每个应用的应该为唯一的,并且要修服务名字:如dubbo_provider为dubbo_provider_1;

    
  <dubbo:service interface="com.java.spring.provider.service.UserService" ref="userServiceImpl" />  

beans>

接口和实现类代码分别为:
UserService接口

public interface UserService {

    public String registerUser(String param);

    public String resetPassword(User user);
    }

UserServiceImpl实现类

/**
 * @author 作者:zhaofq
 * @version 创建时间:2017年1月10日 下午2:13:25 类说明
 */
@Service
public class UserServiceImpl implements UserService {

    private static Logger loger = Logger.getLogger(UserServiceImpl.class.getName());

//  @Autowired
//  UserDao userDao;


    public String registerUser(String param) {
//      Message message = new Message();
           System.out.println("hello--我是provider---------"+ param);
        return  param;
    }

    /* (non-Javadoc)
     * @see com.java.spring.provider.service.UserService#restPassword(com.java.spring.provider.mudles.User)
     */
    public String resetPassword(User user) {
        System.out.println("reset"+user.getIdNumber());
        return "reset"+user.getIdNumber();
    }
}

消费者项目:
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第4张图片
只有controller层,这个需要调用service接口,就需要吧生产者接口打包为jar,然后把jar引入这项目。就可以自动引入了。
controller的代码为:

import com.java.spring.provider.mudles.User;
import com.java.spring.provider.service.UserService;



/**
* @ClassName: Controller
* @Description: TODO(这里用一句话描述这个类的作用)
* @author zhaofq
* @date 2017年6月2日
*/
@Controller
public class UserController {

    @Autowired
    UserService userServiceImpl;
    @RequestMapping(value="/register")
    public @ResponseBody String  userRegister(String param) {
        System.out.println("helllo\n");
        User user = new User();
        user.setIdNumber("999999");
        HttpServletRequest param1= null;
//      userServiceImpl.registerUser(param);
        param = "hahahhah";
        System.out.println(userServiceImpl.resetPassword(user)+"\n");
        return userServiceImpl.registerUser(param);
    }

}

从上面的引入包路径可看出是provider的接口,
配置文件也是本项目的:spring文件中;


<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:mvc="http://www.springframework.org/schema/mvc"  xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <mvc:annotation-driven />
    
    <context:component-scan base-package="com.java.spring.consumer" />

      
   <dubbo:application name="dubbo_consumer" />

     
 
  <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

      
   <dubbo:reference id="userServiceImpl" interface="com.java.spring.provider.service.UserService" />

至此配置代码都完成,先启动zookeeper,然后两个项目在在tomcat中启动,登陆dubbo-admin可以看到
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第5张图片
访问:
http://localhost:8080/consumer/register
jdk1.8+zookeeper+dubbo+spring+springmvc基础环境搭建_第6张图片
基本环境搭建完成;

补充:zookeeper-3.5.3启动提示
org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
显然提示8080端口被占用,但是zookeeper的端口是2181怎么能占用8080呢,官方文档说有用到了jetty启动管制台管理,解决办法是:
1,删除jetty。
2,修改jetty端口(zoo.cfg中添加admin.serverPort=XXXX)。

你可能感兴趣的:(java-web)