從demo 開始學dubbo
dubbo 介紹
dubbo 是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案, 以及SOA服務治理方案.
其核心部分包含:
遠程通信:
提供對多種基於長連接的NIO框架抽象封裝, 包括多種線程模型,序列化, 以及”請求-響應”模式的信息交換方式.
集羣容錯:
提供基於接口方法的透明遠程過程調用, 包括多協議支持,以及軟負載均衡,失敗容錯, 地址路由, 動態配置等集羣支持。
自動發現:
基於註冊中心目錄服務,使服務消費者能動態查找服務提供方, 是地址透明,是服務提供方可以平滑增加或者減少機器。
【引自[dubbo官網介紹](http://dubbo.io/Home-zh.htm】
demo地址
下載三個項目並導入到eclipse中:
- test-service-api
IUserService 服務接口:
package jzq.lz.test_service_api;
public interface IUserService {
UserEntity queryUserEntity();
}
UserEntity:
import java.io.Serializable;
public class UserEntity implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private Integer userId;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserEntity [userName=" + userName + ", userId=" + userId + ", password=" + password + "]";
}
}
- dubbo-demo-consumer
App :
package jzq.lz.test_service_consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import jzq.lz.test_service_api.IUserService;
import jzq.lz.test_service_api.UserEntity;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
IUserService demoService = (IUserService)context.getBean("userService"); // 獲取遠程服務代理
UserEntity user = demoService.queryUserEntity();
System.out.println(user);
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
dubbo-consummer.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="consumer-userService" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://192.168.0.223:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="userService" interface="jzq.lz.test_service_api.IUserService" />
</beans>
pom.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.0</modelVersion>
<groupId>jzq.lz</groupId>
<artifactId>test-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-service-consumer</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<distributionManagement>
<repository>
<id> releases </id>
<name> Nexus Release Repository </name>
<url> http://192.168.0.101:8081/nexus/content/repositories/releases/ </url>
</repository>
<snapshotRepository>
<id> snapshots </id >
<name> Nexus Snapshot Repository </name >
<url> http://192.168.0.101:8081/nexus/content/repositories/snapshots/ </url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jzq.lz</groupId>
<artifactId>test-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
- dubbo-demo-provider
Main:
package jzq.lz.test.service.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class Main
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
UserServiceIml服務是實現:
package jzq.lz.test.service.provider;
import jzq.lz.test_service_api.IUserService;
import jzq.lz.test_service_api.UserEntity;
public class UserServiceIml implements IUserService {
@Override
public UserEntity queryUserEntity() {
UserEntity userEntity = new UserEntity();
userEntity.setPassword("123456");
userEntity.setUserId(123);
userEntity.setUserName("18320928860");
return userEntity;
}
}
dubbo-provider.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-world-app" />
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper註冊中心暴露服務地址 -->
<dubbo:registry address="zookeeper://192.168.0.223:2181" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務接口 -->
<dubbo:service interface="jzq.lz.test_service_api.IUserService" ref="userService" />
<!-- 和本地bean一樣實現服務 -->
<bean id="userService" class="jzq.lz.test.service.provider.UserServiceIml" />
</beans>
運行結果
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
UserEntity [userName=18320928860, userId=123, password=123456]
xml報錯提示
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'.
可參考 esclipse 添加xsd 校驗