转载:http://blog.csdn.net/zjcjava/article/details/78766095
背景
Dubbo的开源人梁飞在内部的交流会上宣布重启dubbo的维护和更新,具体PPT内容请看我的个人订阅号。
因此本文基于2017-12-10号的维护的版本dubbo-2.5.7。
这里为何从Dubbo-admin开始,这不是最重要的部分,但是却是dubbo源码编译最简单的部分。
准备工作
按照zookeeper服务(参考单机安装zookeeper安装)
下载dubbo源码
https://github.com/alibaba/dubbo/releases/tag/dubbo-2.5.7
当然也可以直接从我的github上面直接下载已经编译好了的war包
https://github.com/BambooZhang/dubbo-study
编译
解压后这里只用到一个项目就是dubbo-admin
cd D:git_spacedubbo-dubbo-2.5.7dubbo-dubbo-2.5.7dubbo-admin
mvn package -Dmaven.skip.test=true
出现下面的结果则说明编译成功
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ dubbo-admin ---
[INFO] Packaging webapp
[INFO] Assembling webapp [dubbo-admin] in [D:git_spacedubbo-dubbo-2.5.7dubbo-dubbo-2.5.7dubbo-admin argetdubbo-admin-2.5.7]
[INFO] Processing war project
[INFO] Copying webapp resources [D:git_spacedubbo-dubbo-2.5.7dubbo-dubbo-2.5.7dubbo-adminsrcmainwebapp]
[INFO] Webapp assembled in [880 msecs]
[INFO] Building war: D:git_spacedubbo-dubbo-2.5.7dubbo-dubbo-2.5.7dubbo-admin argetdubbo-admin-2.5.7.war
[INFO] WEB-INFweb.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.516 s
[INFO] Finished at: 2017-12-10T17:38:17+08:00
[INFO] Final Memory: 19M/244M
[INFO] ------------------------------------------------------------------------
部署dubbo-admin到tomcat
把targetdubbo-admin-2.5.7下面的文件都拷贝到tomcat/webapps/ROOT下面
修改配置dubbo.properties
zookeeper地址修改为自己安装的ip地址,默认是127.0.0.1
dubbo.registry.address=zookeeper://192.168.0.91:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
启动dubbo-admin
首先启动zookeeper服务,在启动tomcat
启动完成访问localhost:8080
默认账号密码都是root
Dubbo服务提供者和消费者demo
dubbo-admin-2.5.7.war管理中心
dubbo-provider服务器提供者
dubbo-consumer服务器消费者
spring版本:4.3.9.RELEASE
dubbo-provider服务器提供者
pom
<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.bamboo</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-provider</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.9.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
applicationProvider.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 ">
<!-- 具体的实现bean -->
<bean id="providerService" class="com.bamboo.dubbo_provider.ProviderServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo_provider" />
<!-- 使用multicast广播注册中心暴露服务地址
<dubbo:registry address="multicast://localhost:1234" />-->
<!-- 使用zookeeper注册中心暴露服务地址 127.0.0.1:2181 -->
<dubbo:registry address="zookeeper://192.168.0.91:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.bamboo.service.ProviderService" ref="providerService" />
</beans>
java
service
package com.bamboo.service;
public interface ProviderService {
public String sayHello(String name);
}
ProviderServiceImpl
package com.bamboo.dubbo_provider;
import com.bamboo.service.ProviderService;
public class ProviderServiceImpl implements ProviderService {
public String sayHello(String name) {
return "Hello:"+name+"你好,你好~~";
}
}
main.java
package com.bamboo.dubbo_provider;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* dubbo 服务提供端
* @author bamboo
*/
public class App
{
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("提供者服务已注册成功");
System.out.println("请按任意键取消提供者服务");
try {
System.in.read();//让此程序一直跑,表示一直提供服务
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
提供者服务已注册成功
请按任意键取消提供者服务
dubbo-consumer服务器消费者
pom
<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.bamboo</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-consumer</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.9.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 添加dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加zk客户端依赖 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
applicationConsumer.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="dubbo_consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址127.0.0.1:2181 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.0.91:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="providerService" interface="com.bamboo.service.ProviderService" />
</beans>
java
service
package com.bamboo.service;
public interface ProviderService {
public String sayHello(String name);
}
main
package com.bamboo.dubbo_consumer;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bamboo.service.ProviderService;
/**
* dubbo 服务消费端
* @author bamboo
*
*/
public class App
{
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
ProviderService providerService = (ProviderService) context.getBean("providerService");
System.out.println(providerService.sayHello("bamboo"));
System.out.println("Press any key to exit.");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello:bamboo你好,你好~~
Press any key to exit.
查看监控列表