温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Dubbo如何实现调用

发布时间:2020-08-10 11:24:06 来源:亿速云 阅读:155 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关Dubbo如何实现调用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

 

1、创建maven工程(打包方式为wardubbodemo_provider,在pom.xml文件中导入如下坐标

 

~~~xml

<properties>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <maven.compiler.source>1.8</maven.compiler.source>

  <maven.compiler.target>1.8</maven.compiler.target>

  <spring.version>5.0.5.RELEASE</spring.version>

</properties>

<dependencies>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-beans</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jdbc</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aspects</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-jms</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>${spring.version}</version>

  </dependency>

  <!-- dubbo相关 -->

  <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>dubbo</artifactId>

    <version>2.6.0</version>

  </dependency>

  <dependency>

    <groupId>org.apache.zookeeper</groupId>

    <artifactId>zookeeper</artifactId>

    <version>3.4.7</version>

  </dependency>

  <dependency>

    <groupId>com.github.sgroschupf</groupId>

    <artifactId>zkclient</artifactId>

    <version>0.1</version>

  </dependency>

  <dependency>

    <groupId>javassist</groupId>

    <artifactId>javassist</artifactId>

    <version>3.12.1.GA</version>

  </dependency>

  <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.2.47</version>

  </dependency>

</dependencies>

<build>

  <plugins>

    <plugin>

      <groupId>org.apache.maven.plugins</groupId>

      <artifactId>maven-compiler-plugin</artifactId>

      <version>2.3.2</version>

      <configuration>

        <source>1.8</source>

        <target>1.8</target>

      </configuration>

    </plugin>

    <plugin>

      <groupId>org.apache.tomcat.maven</groupId>

      <artifactId>tomcat7-maven-plugin</artifactId>

      <configuration>

        <!-- 指定端口 -->

        <port>8081</port>

        <!-- 请求路径 -->

        <path>/</path>

      </configuration>

    </plugin>

  </plugins>

</build>

~~~

 

2、配置web.xml文件

 

~~~xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>Archetype Created Web Application</display-name>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext*.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

</web-app>

 

~~~

 

3、创建服务接口

 

~~~java

package com.itheima.service;

public interface HelloService {

    public String sayHello(String name);

}

~~~

 

4、创建服务实现类

 

~~~java

package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.service.HelloService;

 

@Service

public class HelloServiceImpl implements HelloService {

    public String sayHello(String name) {

        return "hello " + name;

    }

}

~~~

 

注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务

 

5、在src/main/resources下创建applicationContext-service.xml

 

~~~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:p="http://www.springframework.org/schema/p"

                   xmlns:context="http://www.springframework.org/schema/context"

                   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

             xmlns:mvc="http://www.springframework.org/schema/mvc"

                   xsi:schemaLocation="http://www.springframework.org/schema/beans

                   http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/mvc

         http://www.springframework.org/schema/mvc/spring-mvc.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">

         <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->

         <dubbo:application name="dubbodemo_provider" />

         <!-- 连接服务注册中心zookeeper ipzookeeper所在服务器ip地址-->

         <dubbo:registry address="zookeeper://192.168.134.129:2181"/>

         <!-- 注册  协议和port   端口默认是20880 -->

         <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>

         <!-- 扫描指定包,加入@Service注解的类会被发布为服务  -->

         <dubbo:annotation package="com.itheima.service.impl" />

</beans>

~~~

 

6、启动服务

 

tomcat7:run

看完上述内容,你们对Dubbo如何实现调用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI