MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

一:安装软件:
tomcat
Zookeeper
Dubbo+admin

二:工程:

总工程
  API
    Pom.xml:不用引用任何东西
  Provider
    Pom.xml:要denpendency API的模块。
    provider.xml:向dubbo注册所提供的服务。
  Consumer
    Pom.xml:要denpendency API的模块。
    consumer.xml:向dubbo注册。
  Pom.xml:dependency:Spring    Dubbo    Zookeeper

三:具体步骤:

1. 安装tomcat。这个可以自己在网上找。修改名称为tomcat,放/usr/local/下边。然后启动tomcat(bin/startup.sh)。

2. Zookeeper,

a. 下载(http://mirrors.shuosc.org/apache/zookeeper/),然后修改名称为zookeeper,拷贝到/usr/local/文件夹下。

b. 配置、启动zookeeper,在命令行中,进入/usr/local/zookeeper/conf文件夹,修改zoo_sample.cfg为zoo.cfg,然后返回上个目录,在命令行中执行 bin/zkServer.sh start。

3.

a. 安装Dubbo+admin,用于管理zookeeper,下载(http://dubbo.io/),进入dubbo+admin中,用idea打开pom.xml文件,然后在idea的终端中,打包 mvn install -DskipTests

b. 进入dubbo-admin->targer文件夹下,找到dubbo-admin-2.5.5.war文件,拷贝到/usr/local/tomcat/webapps/文件夹下,然后可以直接在浏览器中访问改地址。

c. 一般地址是http://localhost:8080/dubbo-admin-2.5.5/,用户名密码都是root,root。

http://m.blog.csdn.net/think12/article/details/78162971

详细讲解IntellJ 下Dubbo +zookeeper+springmvc的配置

http://blog.csdn.net/guyang5863637/article/details/62041425

zookeeper已经开启

dubbo-admin安装成功

1、开始搭建一个空maven工程 作为父工程管理其他模块

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

2、创建子工程 MyController(消费者). MyInterfence.   MyService(提供者)

MyController可以使用maven模版, MyInterfence.   MyService不使用maven模版

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

MyInterfence.   MyService的maven创建省略,不需要使用模版

3、MyInterfence项目.的java目录下创建包com.start.interfence  创建接口MyInterfence,如下

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

MyInterfence的简单代码

 package com.start.interfence;

 public interface MyInterfence {
public String helloWorld();
}

4、MyService项目

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

首先在pom.xml中引入依赖的jar包

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Demo</artifactId>
<groupId>com.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>MyService</artifactId> <dependencies>
<dependency>
<groupId>com.parent</groupId>
<artifactId>MyInterfence</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies> </project>

然后在resources目录下创建provider.xml (Spring的xm l)

 <?xml version="1.0" encoding="ISO-8859-1"?>

 <beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.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-3.1.xsd"
default-lazy-init="false" > <bean id="myInterfence" class="com.start.service.MyInterfenceImpl"></bean> <dubbo:application name="dubbo_provider"></dubbo:application> <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" ></dubbo:registry> <dubbo:service interface="com.start.interfence.MyInterfence" ref="myInterfence" /> </beans>

在java目录下创建包com.start.service。   在此包下创建MyInterfence.java 实现MyInterfence接口,MyInterfence.java代码如下

 package com.start.service;

 import com.start.interfence.MyInterfence;

 public class MyInterfenceImpl implements MyInterfence{
public String helloWorld() {
return "Hello 你好";
}
}

在java目录的包com.start.service。下创建Provider.java类 用于启动服务 Provider.java代码如下

 package com.start.service;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import java.io.IOException;

 public class Provider {

     public static void main(String[] args){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
context.start();
System.out.println("provider 服务开启");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}

此时,打开网页http://localhost:8080/dubbo-admin-2.5.8/  (dubbo-admin-2.5.8根据你的配置)

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

5、配置MyController项目,

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

首先,在resources目录下新建consumer.xml配置文件,配置SpringMVC注解,视图解析器,dubbo。  consumer.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:component-scan base-package="com.start.controller"/> <!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/> <!-- 视图解析器 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean> <dubbo:application name="dubbo_consumer"></dubbo:application> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry> <!-- 要引用的服务 -->
<dubbo:reference interface="com.start.interfence.MyInterfence" id="myInterfence"></dubbo:reference> </beans>

然后在web.xml添加配置信息,注意标签的顺序

 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:consumer.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>

在java目录下新建包com.start.controller 并在此包下新建MyController.java 测试消费者 代码如下

 package com.start.controller;

 import com.start.interfence.MyInterfence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController {
@Autowired
private MyInterfence myInterfence;
@RequestMapping("/test.do")
public String getTest() {
System.out.print("11111111111");
String str = myInterfence.helloWorld();
System.out.print(str);
return "welcome";
} /*public static void main(String[] args){
//ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
context.start();
System.out.println("消费者。。。");
MyInterfence my= (MyInterfence) context.getBean("myInterfence");
my.helloWorld();
}*/
}

在WEB-INF 下建view/welcome.jsp

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>welcome China</title>
</head>
<body>
"sssss"
</body>
</html>

配置Tomcat,点击下图进行Tomcat的配置,点击左上角的➕选择下面的Tomcat Server ,然后选择Local

然后选择右侧的Deployment,点击左下角的➕,选择Artifcat,结果如图3所示

图1

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

图2

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

图3

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

启动Tomcat

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

此时会报错,显示404 因为在后台开了一个Tomcat了,端口号为8080,开着的目的,主要是打开dubbo的admin页面,查看提供者和消费者的

把端口号换一下,重新运行

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

打开界面,输入配置的路径,然后输入请求路径,成功

http://localhost:8085/demo/test.do

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

登录到dubbo的admin页面,发现消费者也出来了

MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

一个简单的小项目就完成了。 
注意,在写提供者那一块,Zookeeper的服务就要打开,放那不用管它,Tomcat也可以本地先启动,为的是能够打开dubbo的admin,当然,你在Tomcat本地上先要把dubbo.war包解压后放到webapp目录下,也可以解压替换root文件夹,这样一打开tomcat首页就是dubbo-admin页面,注意后来发布项目的时候,Tomcat的端口号不要再用8080了,容易冲突。 
第一次尝试很好的例子

本文的源代码https://github.com/wanglecp/Demo.git

上一篇:ajax调用servlet


下一篇:JQuery-事件(部分)