Java 项目调用 dubbo 服务

最近,时光有点荒废。书也没怎么读,博客也不更新。反省一分钟…

前几天写工具的时候,需要调用一个dubbo服务,但之前是配置的简单的Java项目,所以这边简单记载一下这个过程。

  • 首先新建一个Java的 maven 项目,以下是pom文件,dubbo要基于Spring搞事的。
<?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>cn.xxxx</groupId>
    <artifactId>dubbo-test-check</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <org.springframework.version>3.2.14.RELEASE</org.springframework.version>
    </properties>

    <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cn.xxxx.test.DubboTest</mainClass>
                        </manifest>
                    </archive>
                    <classesDirectory>
                    </classesDirectory>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <!-- 将依赖一起打包到 JAR -->
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>cn.xxxx.test.DubboTest</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.14.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>
</project>
  • Dubbo配置相关,
<?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="webank_check" />
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry  address="zookeeper://192.168.0.1:2181?backup=192.168.0.2:2181,192.168.0.3:2181"/>

    <dubbo:reference id="xxxxxApi"
                     interface="com.xxxx.webank_api.xxxxxApi"
                     version="1.0.0" check="false" timeout="10000"
                     group="product"/>

    <bean id="weBankTicket" class="cn.xxxx.test.WeBankTicket">
        <property name="xxxxx" ref="xxxxxApi"/>
    </bean>
</beans>
  • 主要的DubboTest类
public class DubboTest {
    public static void main(String[] args) {
        try {
            DubboTest dubboTest = new DubboTest();

            ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            WeBankTicket weBankTicket = (WeBankTicket) ac.getBean("weBankTicket");
            String ticket = weBankTicket.getTicket(APP_ID);
            ...
            ...
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 操作类
public class WeBankTicket {

    xxxxxApi xxxxx;

    public void setxxxxx(xxxxxApi xxxxx) {
        this.xxxxx = xxxxx;
    }

    public xxxxxApi getxxxxx() {
        return xxxxx;
    }

    public String getTicket(String appId) {
        WeBankWeChatTicketReq weBankWeChatTicketReq = new WeBankWeChatTicketReq();
        weBankWeChatTicketReq.setAppId(appId);
        if (null == weBankWeChatTicket) {
            System.out.println("======================");
        }
        WeBankWeChatTicketResp weBankWeChatTicketResp = xxxxx.getSignTicket(weBankWeChatTicketReq);
        return xxxxx.getSignTicket();
    }
}

因为是一个消费服务,主要是从zookeeper中拿到接口调用就行。没开始注解,所以bean配置麻烦了点。算是一个小工具吧。如果有本地小工具调用生产dubbo的时候,可以这么做。

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.