Maven多モジュールを使ってdubboの最初の分散ハローワールドを構築する.
なぜMaven多モジュールでプロジェクトを構築するかというと、dubboとは何をするかというと多くなく、直接に開始します.
まず、mavenプロジェクトを作成し、rootモジュールとしてmydubboと命名し、srcディレクトリを削除します.
Springが必要です.zookeeperの依存を除いて.
mydubro->pom.xml
myService:機能インターフェースモジュール
myProvider:dubbo提供者
myConsmer:dubbo消費者
消費者とプロバイダの機能インターフェースが一致しているため、myServiceモジュール(myServiceのプロジェクトpackingはjar)に依存しています.
myProvider->pom.xml
ハローService.java
myProviderの依存はmyServiceが生み出すjarがあります.
今からdubboプロバイダmyProviderのコードを作成します.
まず、ハローサービスの実現が必要です.
ハローServiceImpl.java
Spring構成で暴露サービスを宣言して、次のようにprovider.xmlを作ります.
最後に、私達は提供者サービスを開始できます.スタートクラスは以下の通りです.
そして、消費者がdubboを通じて以下の遠隔サービスを調整するようにしましょう.
消費者はSpring配置で遠隔サービスのspringプロファイルを引用して、以下の通りである.
consumer.xml
ConsmerCient.java
リモートサービスの呼び出しが成功したことが分かります.
まず、mavenプロジェクトを作成し、rootモジュールとしてmydubboと命名し、srcディレクトリを削除します.
Springが必要です.zookeeperの依存を除いて.
mydubro->pom.xml
<?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</groupId>
<artifactId>mydubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<modules>
<module>myProvider</module>
<module>myConsumer</module>
<module>myService</module>
</modules>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--dubbo -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--zookeeper -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<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>
</dependencies>
</project>
そして三つのmavenサブモジュールを作成します.それぞれはmyService、myProvider、myConsmerです.myService:機能インターフェースモジュール
myProvider:dubbo提供者
myConsmer:dubbo消費者
消費者とプロバイダの機能インターフェースが一致しているため、myServiceモジュール(myServiceのプロジェクトpackingはjar)に依存しています.
myProvider->pom.xml
<?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">
<parent>
<artifactId>mydubbo</artifactId>
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>myProvider</artifactId>
<dependencies>
<dependency>
<groupId>com</groupId>
<artifactId>myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
myConsmer->pom.xml<?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">
<parent>
<artifactId>mydubbo</artifactId>
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>myConsumer</artifactId>
<dependencies>
<dependency>
<groupId>com</groupId>
<artifactId>myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
myService->pom.xml<?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">
<parent>
<artifactId>mydubbo</artifactId>
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>myService</name>
<artifactId>myService</artifactId>
<packaging>jar</packaging>
</project>
myServiceの中でInterfaceを創立して、HelloServiceと命名して、具体的には以下の通りです.ハローService.java
package com.service;
/**
* Created by yuyufeng on 2016/11/16.
*/
public interface HelloService {
String speakHello(String name);
}
今、私たちはinstall mysServiceというプロジェクトが必要で、他の引用者のために頼れるjarが生まれました.myProviderの依存はmyServiceが生み出すjarがあります.
今からdubboプロバイダmyProviderのコードを作成します.
まず、ハローサービスの実現が必要です.
ハローServiceImpl.java
package com.service.impl;
import com.service.HelloService;
/**
* Created by yuyufeng on 2016/11/16.
*/
public class HelloServiceImpl implements HelloService {
public String speakHello(String name) {
return " :" + name;
}
}
を使用してくださいSpring構成で暴露サービスを宣言して、次のように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-provider" />
<!-- multicast -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- dubbo 20880 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- -->
<dubbo:service interface="com.service.HelloService" ref="helloService" />
<!-- bean -->
<bean id="helloService" class="com.service.impl.HelloServiceImpl" />
</beans>
今回dubboで使用する登録センターはzookeeperですので、ローカルでzookeeperサービスを起動する必要があります.最後に、私達は提供者サービスを開始できます.スタートクラスは以下の通りです.
package com.service.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* Created by yuyufeng on 2016/11/16.
*/
public class ProviderServer{
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
//
System.in.read();
}
}
起動後、私達はzookeeper登録センターで私達のサービスプロバイダがすでに登録されているのを見ることができます.そして、消費者がdubboを通じて以下の遠隔サービスを調整するようにしましょう.
消費者はSpring配置で遠隔サービスのspringプロファイルを引用して、以下の通りである.
consumer.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" />
<!-- multicast -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- , bean demoService -->
<dubbo:reference id="helloService" interface="com.service.HelloService" />
</beans>
消費者起動類ConsmerCient.java
import com.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by yuyufeng on 2016/11/16.
*/
public class ConsumerClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
String result = helloService.speakHello("yyf");
System.out.println(result);
}
}
運転改類の結果は以下の通りです.リモートサービスの呼び出しが成功したことが分かります.