首页 > 学院 > 开发设计 > 正文

dubbo学习之服务提供者

2019-11-15 00:41:48
字体:
来源:转载
供稿:网友
dubbo学习之服务提供者

1、简介

  这里主要记录如何搭建一个sPRing框架,提供一个dubbo服务,包括详细的步骤。

2、详细步骤

  2.1 项目目录结构

        

  2.2 创建maven项目

    new --> Web Project ,选中Add maven support,添加maven支持,详细如下:

    

    

    

    

    

    创建maven项目完成,可以部署到tomcat下,测试一下是否能访问。

  2.3 添加maven依赖

    修改pom.xml,加载基础的spring jar包 和 dubbo的jar 包,如下:    

<properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <spring-version>4.1.6.RELEASE</spring-version>  </properties>    <dependencies>      <dependency>          <groupId>org.springframework</groupId>          <artifactId>spring-core</artifactId>          <version>${spring-version}</version>      </dependency>      <dependency>          <groupId>org.springframework</groupId>          <artifactId>spring-context</artifactId>          <version>${spring-version}</version>      </dependency>      <dependency>          <groupId>org.springframework</groupId>          <artifactId>spring-web</artifactId>          <version>${spring-version}</version>      </dependency>      <dependency>      <groupId>com.alibaba</groupId>      <artifactId>dubbo</artifactId>      <version>2.6.2-SNAPSHOT</version>    </dependency>        </dependencies>

  2.4 修改applicationContext-base.xml配置文件,增加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:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:d="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">        <!-- 提供方应用信息,用于计算依赖关系 -->    <d:application name="dubbo_provider"  />    <!-- 用dubbo协议在20880端口暴露服务 -->    <d:protocol name="dubbo" port="20880" />    <!-- 使用zookeeper集群注册中心暴露服务地址 -->   <!-- 这里是用的伪集群方式,可参考“zookeeper初始*********”博客部分  -->    <d:registry address="zookeeper://192.168.1.102:2181?backup=192.168.1.102:2182,192.168.1.102:2183" default="true" />        <!-- 声明需要暴露的服务接口 -->    <d:service interface="com.wei.interfaces.DemoService" ref="demoService" />    <!-- 和本地服务一样实现服务 -->        <bean id="demoService" class="com.wei.services.DemoServiceImpl" />    </beans>

  2.5 修改web.xml,如下:    

  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:*.xml</param-value>  </context-param>

  2.6 编写对外发布的dubbo接口,如下:  

package com.wei.interfaces;/** * 单独打包 * @author wed * */public interface DemoService {    public String sayHello(String name);}

  2.7 编写对外发布dubbo接口的实现类,如下:

package com.wei.services;import com.wei.interfaces.DemoService;public class DemoServiceImpl implements DemoService {    @Override    public String sayHello(String name) {        return "hello "+ name;    }}

  2.8 启动服务,查看dubbo管理控制台,如下:

    可以看到,在提供者tab页面已经有ip信息,点击进去可以看到对应的服务信息。

2.8 注意点:

    (1)把interfaces单独达成jar包,后面消费者要用到。

    (2)这一步用到的zookeeper地址,是前面配好的zookeeper伪集群方式;如果没有配置多个节点,也可以配置为单节点方式,或者是multicast注册中心。

  


上一篇:JAVA &amp;&amp; JSON

下一篇:登录拦截功能

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表