Maven_Dubbo 实例

来源:互联网 发布:idea修改js不重启 编辑:程序博客网 时间:2024/06/09 19:19

这里写图片描述

DemoServer.java

package com.demo.dubbo.service;public interface DemoServer {    String sayHello(String str);}

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="consumer-dubbo" />           <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry  protocol="zookeeper"  address="192.168.216.132:2181" />            <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->    <dubbo:reference id="demoService" interface="com.demo.dubbo.service.DemoServer" /></beans>

log4j.properties

log4j.appender.Stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.Stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\nlog4j.rootLogger=INFO,Stdoutlog4j.logger.org.apache.wicket=INFOlog4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFOlog4j.logger.org.apache.wicket.version=INFOlog4j.logger.org.apache.wicket.RequestCycle=INFO

DubboConsumer.java

package com.demo.dubbo.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.demo.dubbo.service.DemoServer;public class DubboConsumer {    public static void main(String[] args) throws InterruptedException {        int i = 0;        while (i++ < 10) {            SayHello();            Thread.sleep(5000);        }    }    /**     * 客户端方法中调用服务端的service     */    public static void SayHello() {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "applicationConsumer.xml" });        context.start();        DemoServer demoServer = (DemoServer) context.getBean("demoService");        System.err.println(demoServer.sayHello("你好,Dubbo-Provider"));    }}

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>com.demo</groupId>    <artifactId>maven-dubbo-consumer</artifactId>    <version>0.0.1-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.3.0.RELEASE</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.10</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.10</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.5.1</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>                <version>3.0.1</version>                <executions>                    <execution>                        <id>attach-sources</id>                        <goals>                            <goal>jar</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

maven-dubbo-consumer 实例源码


DemoServer.java

package com.demo.dubbo.service;public interface DemoServer {    String sayHello(String str);}

DemoServerImpl.java

package com.demo.dubbo.service.impl;import com.demo.dubbo.service.DemoServer;public class DemoServerImpl implements DemoServer {    public String sayHello(String str) {        str = "你好,Dubbo-Consumer:" + str;        System.out.println(str);        return str;    }}

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        ">        <!-- 提供方应用信息,用于计算依赖关系 -->     <dubbo:application name="provider-dubbo" />    <!-- 使用zookeeper注册中心暴露服务地址-->         <dubbo:registry protocol="zookeeper"  address="192.168.216.132:2181"  />    <!-- 使用dubbo协议在20880端口暴露服务-->     <dubbo:protocol name="dubbo" port="20880" />       <!-- 声明需要暴露的服务接口 -->      <dubbo:service interface="com.demo.dubbo.service.DemoServer"        ref="demoService" />           <!-- 具体bean的实现-->    <bean id="demoService" class="com.demo.dubbo.service.impl.DemoServerImpl" /></beans>

log4j.properties

log4j.appender.Stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.Stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\nlog4j.rootLogger=INFO,Stdoutlog4j.logger.org.apache.wicket=INFOlog4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFOlog4j.logger.org.apache.wicket.version=INFOlog4j.logger.org.apache.wicket.RequestCycle=INFO

DubboProvider.java

package com.demo.dubbo.test;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class DubboProvider {    public static void main(String[] args) throws IOException {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "applicationProvider.xml" });        System.out.println("按任意键退出");        System.in.read();    }}

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>com.demo</groupId>    <artifactId>maven-dubbo-provider</artifactId>    <version>0.0.1-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.12</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.3.0.RELEASE</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.10</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.10</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.5.1</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>                <version>3.0.1</version>                <executions>                    <execution>                        <id>attach-sources</id>                        <goals>                            <goal>jar</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

maven-dubbo-provider 实例源码