common-if工程单独打包,dubbo服务提供端和消费端共享。
common-if工程很简单只定义了一个接口和一个User类,利用maven构建工具打成一个jar包:common-if-1.0-SNAPSHOT.jar
dubbo-provide工程为服务提供端,引入了dubbo和zk的依赖,pom.xml,并且引入上面的common-if-1.0-SNAPSHOT.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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hd</groupId>
<artifactId>dubbo-provide</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>${project.artifactId}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxxxx.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.0.7.RELEASE</spring.version>
<logback.version>1.1.5</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-simple</artifactId>-->
<!--<version>1.7.22</version>-->
<!--</dependency>-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
</project>
public class UserServiceImpl implements UserService {
@Override
public String print() {
return "hello dubbo";
}
@Override
public String getUserName(User user) {
return user.getUserName();
}
}
applicationContext.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="dubbo-provide" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务,访问日志路径-->
<dubbo:protocol name="dubbo" port="20880" accesslog="xxxxxxxxxxxx.log"/>
<!-- 用rmi协议在20881端口暴露服务 -->
<dubbo:protocol name="rmi" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.my.service.UserService"
ref="userService" protocol="dubbo" delay="-1" timeout="6000" retries="0"/>
<!-- 和本地bean一样实现服务 -->
<bean id="userService" class="com.vip.service.UserServiceImpl" />
</beans>
public class Main {
final static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws IOException {
logger.info("dubbo服务端正在启动……");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
System.in.read(); // 只是不让服务挂掉。。。
}
}
dubbo-comsumer工程为消费端,一样引入了dubbo和zk的依赖,pom.xml,并且引入上面的common-if-1.0-SNAPSHOT.jar
applicationContext.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"
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="hello_consumer"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 生成远程服务代理,可以像使用本地bean一样使用 -->
<dubbo:reference id="userService" interface="com.my.service.UserService"/>
</beans>
public class Main {
final static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
logger.info("作业启动……");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
logger.info("作业启动成功……");
UserService userService = (UserService)context.getBean("userService");
System.out.println(userService.print());
User user = new User();
user.setUserName("dubbo");
String userName = userService.getUserName(user);
System.out.println(userName);
}
}
下载zookeeper注册中心,官网地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 下载后解压即可,找到解压目录的bin目录运行zkServer.cmd,启动注册中心。