转载自https://blog.csdn.net/noaman_wgs/article/details/70214612(本文只是针对该链接的项目的搭建和运行进行步骤的详细化,感谢!!!)
dubbo的github地址:https://github.com/apache/incubator-dubbo(可下载整个压缩包,解压搜索xsd,就能找到dubbo.xsd文件)
zookeeper3.4.10下载地址:https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.10/(解压到本地,并进入其conf目录,重命名zoo_sample.cfg为zoo.cfg即可,其余无需改动。)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
新建完,项目报错。
点击项目,右键,Properties,找到Project Facets,并把Dynamic Web Module的勾去掉,并Apply应用。
然后再选择Dynamic Web Module版本为2.5并勾选上,底下会出现,Further configuration available,点击进入。
在dubbo-api底下输入框输入/src/main/webapp
,并点击OK,然后再次点击OK,这样就自动生成了web-inf目录和web.xml文件。
(还有一种简单的方法就是在/src/main/webapp
目录下手动新建web-inf目录和web.xml文件)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.mygroupId>
<artifactId>DubboDemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>dubbo-apimodule>
<module>dubbo-providermodule>
<module>dubbo-consumermodule>
modules>
<properties>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<spring.version>4.2.5.RELEASEspring.version>
<slf4j.version>1.7.18slf4j.version>
<log4j.version>1.2.17log4j.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.9version>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.10version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
exclusions>
dependency>
dependencies>
project>
com.alibaba.dubbo.demo
,并创建一个接口文件DemoService,代码如下:package com.alibaba.dubbo.demo;
import java.util.List;
public interface DemoService {
List getPermissions(Long id);
}
<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">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.mygroupId>
<artifactId>DubboDemoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>dubbo-providerartifactId>
<dependencies>
<dependency>
<groupId>com.mygroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
com.alibaba.dubbo.demo.impl
,并创建class文件名为DemoServiceImpl,实现DemoService接口,代码如下:package com.alibaba.dubbo.demo.impl;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public List getPermissions(Long id) {
List demo = new ArrayList();
demo.add(String.format("Permission_%d", id - 1));
demo.add(String.format("Permission_%d", id));
demo.add(String.format("Permission_%d", id + 1));
return demo;
}
}
package com.alibaba.dubbo.demo.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
System.err.println(context.getDisplayName() + ": here");
context.start();
System.err.println("服务已经启动...");
System.in.read();
}
}
provider.xml:
<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-provider" owner="programmer" organization="dubbox"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
<bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
beans>
log4j.xml:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
layout>
appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
root>
log4j:configuration>
<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">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.mygroupId>
<artifactId>DubboDemoartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>dubbo-consumerartifactId>
<dependencies>
<dependency>
<groupId>com.mygroupId>
<artifactId>dubbo-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
com.alibaba.dubbo.consumer
,并创建class文件Consumer,代码如下:package com.alibaba.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer {
public static void main(String[] args) {
//测试常规服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.err.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.err.println("consumer");
System.out.println(demoService.getPermissions(1L));
}
}
<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"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
beans>
log4j.xml:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
layout>
appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
root>
log4j:configuration>
如果在eclipse里关闭运行的进程,在zkServer.cmd窗口也能看到关闭信息。