Dubbo入门,搭建Dubbo 测试服提供与消费

来源:互联网 发布:autodesk 360 mac卸载 编辑:程序博客网 时间:2024/05/16 06:16
  • 1.首先安装zookeeper,网上很多资料,略过

  • 2.下载并且安装tomcat linux版本

下载地址:http://tomcat.apache.org/download-70.cgi

上传到服务器,并且解压

[root@lijie java]# tar -xzvf apache-tomcat-7.0.29.tar.gz
  • 3.下载dubbo-admin的war包

dubbo-admin-2.5.4-SNAPSHOT.war ,链接地址:http://download.csdn.net/detail/evankaka/9054273

  • 4.部署dubbo-admin
    [root@lijie java]# cp ./dubbo-admin-2.5.4-SNAPSHOT.war ./apache-tomcat-7.0.29/webapps/    [root@lijie java]# ./apache-tomcat-7.0.29/bin/startup.sh 

注意: 修改WEB-INFO下面的dubbo.properties的zookeeper地址等,并重启tomcat

  • 5.在浏览器中访问

在浏览器上面访问地址:http://192.168.80.123:8080/dubbo-admin-2.5.4-SNAPSHOT

之后会让你输入账号密码:默认账号root 默认密码root

这里写图片描述

登入之后:

这里写图片描述

  • 6.创建两个maven工程

一个服务消费,一个服务提供,工程图如下:

这里写图片描述

  • 6.1 dubbo-pp 服务提供

DubboServer

package com.lijie.producer;public interface DubboServer{    String hello();}

DubboServerImpl

package com.lijie.producer.impl;import org.springframework.stereotype.Service;import com.lijie.producer.DubboServer;@Service("dubboServerImpl")public class DubboServerImpl implements DubboServer{    @Override    public String hello() {        System.out.println("被訪問!");        return "world!";    }}

MainApp

package com.lijie.producer.main;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                "classpath*:applicationContext.xml");        context.start();        System.out.println("begin");        try {            System.in.read();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

applicationContext

<?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"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:aop="http://www.springframework.org/schema/aop"    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        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util.xsd"        >    <dubbo:application name="lijie-dubbo-server"  />    <dubbo:protocol name="dubbo" port="20881" serialization="hessian2" />    <dubbo:registry address="zookeeper://192.168.80.123:2181" check="false"/>    <dubbo:service interface="com.lijie.producer.DubboServer" ref="dubboServerImpl"/>    <dubbo:annotation package="com.lijie.producer" />    <context:component-scan base-package="com.lijie.produce" /></beans>

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>dubbo-pp</groupId>  <artifactId>dubbo-pp</artifactId>  <version>0.0.1-SNAPSHOT</version>  <dependencies>         <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.4.RELEASE</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.4.10</version>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.3</version>        </dependency>        </dependencies>        <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build></project>
  • 6.2 dubbo-cc服务消费

MainApp

package com.lijie.client;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.lijie.producer.DubboServer;@ContextConfiguration(locations={"classpath:applicationContext.xml"})@RunWith(SpringJUnit4ClassRunner.class)public class MainApp {    @Resource    private DubboServer ds;    @Test    public void testServer() throws InterruptedException{        System.out.println("client begin");        String str = ds.hello();        System.out.println("hello "+ str);        Thread.sleep(2000000);        System.out.println("client end");    }}

applicationContext

<?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"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:aop="http://www.springframework.org/schema/aop"    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        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util.xsd"        >    <dubbo:application name="lijie-dubbo-client"  />    <dubbo:registry address="zookeeper://192.168.80.123:2181" check="false"/>    <dubbo:reference id="dubboServerImpl" interface="com.lijie.producer.DubboServer" /></beans>

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>dubbo-cc</groupId>  <artifactId>dubbo-cc</artifactId>  <version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>dubbo-pp</groupId>  <artifactId>dubbo-pp</artifactId>  <version>0.0.1-SNAPSHOT</version></dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>3.2.3.RELEASE</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>3.2.4.RELEASE</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.4.10</version>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.3</version>        </dependency>        </dependencies>        <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build></project>
  • 7.启动服务提供

这里写图片描述

查看dubbo-admin

这里写图片描述

  • 8.启动服务消费

这里写图片描述

查看dubbo-admin

这里写图片描述

0 0
原创粉丝点击