Java 项目调用 dubbo 服务

栏目: Java · 发布时间: 6年前

内容简介:最近,时光有点荒废。书也没怎么读,博客也不更新。反省一分钟…前几天写工具的时候,需要调用一个dubbo服务,但之前是配置的简单的Java项目,所以这边简单记载一下这个过程。因为是一个消费服务,主要是从zookeeper中拿到接口调用就行。没开始注解,所以bean配置麻烦了点。算是一个小工具吧。如果有本地小工具调用生产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的时候,可以这么做。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Nginx Essentials

Nginx Essentials

Valery Kholodkov / Packt Publishing / 2015-7-1 / USD 34.99

About This Book Learn how to set up, configure, and operate an Nginx installation for day-to-day useExplore the vast features of Nginx to manage it like a pro, and use them successfully to run your......一起来看看 《Nginx Essentials》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具