- Tomcat+Dubbo安装
1.将tomcat的webapps目录下的所有文件清空,讲Dubbo管理控制台的程序dubbo-admin-2.5.3.war放
到webapps中,并且解压命名为ROOT。
2.配置dubbo.properties
将以下地址改为你Zookeeper注册的地址。前提:已经安装过Zookeeper。
dubbo.registry.address=zookeeper://192.168.137.2:2181
3.启动tomcat
浏览:192.168.137.2:8080出现以下界面
相应的配置已经配置好了,接下来开始写代码,看一下dubbo是怎么样在Zookeeper上注册服务的,在此举一个provider的例子:
建立一个maven项目,名称:Interfaces
建立一个类:
<span style="color:#666666;"> package com.demo.rpc;
public interface SpeakInterface
{
String speak(People people);
}
</span>
再建一个maven项目,名称:provider
pom文件:
注意:这里在pom文件中引用interface
<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.demo.dubbo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jikexueyuan.rpc</groupId>
<artifactId>http-interfaces</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在src/main/resources路径下建立META-INF/spring文件夹,建立以下文件
Spring-context.xml文件
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.demo.rpc.dubbo.impl" />
<context:annotation-config />
</beans>
Spring-dubbo-provider.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-dubbo-provider"/>
<dubbo:registry id="zk1" address="192.168.137.2:2181" protocol="zookeeper"/>
<dubbo:protocol id="mydubbo" name="dubbo" port="20886"/>
<dubbo:provider registry="zk1" protocol="mydubbo"/>
<!-- <bean id="speakInterface" class="com.demo.rpc.dubbo.impl.SpeakInterfaceImpl" ></bean> -->
<dubbo:service interface="com.demo.rpc.SpeakInterface" ref="speakInterface"/>
<dubbo:monitor protocol="registry" />
</beans
SpeakInterfaceImpl类
import com.demo.rpc.GrowUpInterface;
import com.demo.rpc.People;
import com.demo.rpc.SpeakInterface;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 2015/11/8
*/
@Component("speakInterface")
public class SpeakInterfaceImpl implements SpeakInterface {
/* @Resource
private GrowUpInterface growUpInterface;*/
public String speak(People people) {
return "dubbo return ";
}
}
启动,出现如下界面,说明服务已经注册上