dubbo服务基本搭建部署操作

dubbo服务基本搭建部署操作

1. 启动zookeeper:

(1)zookeeper下载地址http://zookeeper.apache.org/

(2)解压并修改配置

进入到zookeeper的conf目录,找到zoo_sample.cfg文件将其重命名为zoo.cfg,然后打开该文件,
将其中的dataDir和dataLogDir修改如下:

dataDir=E:\\test-project\\dubbo-demo\\data
dataLogDir=E:\\test-project\\dubbo-demo\\log

 2.dubbo接口服务api项目(其实该步上传jar包到Nexus可以用provider的jar包):

注意:将其pom.xml中的打包方式改为jar;

public interface DemoService {
	String sayHello(String name);
}

【pom.xml配置】:


<?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>com.dubbo.api</groupId>
  <artifactId>api</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>api Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>api</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <!--上传配置Nexus 注册仓储地址-->
  <distributionManagement>
    <repository>
      <id>releases</id>
      <url>http://.../nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <url>http://.../nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
</project>

3.dubbo服务提供者provider项目: 

 【pom.xml】配置三个核心依赖:

<!--api接口依赖-->
    <dependency>
      <groupId>com.dubbo.api</groupId>
      <artifactId>api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->

    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
    </dependency>

    <!--zookeeper客服端依赖-->
    <dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.10</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.5.RELEASE</version>
    </dependency>

 【接口实现类】:

public class DemoServiceImpl implements DemoService {
	@Override
	public String sayHello(String name) {
		return "Hello " + name;
	}
}

【provider.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: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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="demo-provider" owner="nz" organization="dubbox"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="com.dubbo.api.service.DemoService" ref="demoService" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="demoService" class="com.dubbo.provider.service.impl.DemoServiceImpl"/>
</beans>

【Provider用于启动项目Java代码】:启动服务提供者

public static void main(String[] args) throws IOException {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
	context.start();
	System.out.println("dubbo服务提供端已启动....");
	// 按任意键退出
	System.in.read();
}

4.dubbo消费者consumer项目:  

 【pom.xml】配置三个核心依赖等同于 服务提供者:

 【Consumer用于启动项目Java代码】:启动消费者:

public static void main(String[] args) throws IOException {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "consumer.xml" );
	context.start();
	System.out.println("dubbo服务消费端已启动...");
	// 获取远程服务代理
	DemoService demoService = (DemoService)context.getBean( "demoService" );
	// 执行远程方法
	String hello = demoService.sayHello( "world" );
	// 显示调用结果
	System.out.println(hello);
	// 按任意键退出
	System.in.read();
}

【consumer.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: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="demo-consumer" owner="nz" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="demoService" interface="com.dubbo.api.service.DemoService"/>
</beans>

5.dubbo-admin管理控制台:

通过dubbo-admin可以更好的管理dubbo服务,将dubbo-admin-2.6.0.war复制到tomcat的webapps目录下。然后启动tomcat,访问
http://localhost:8080/dubbo-admin-2.6.0/(用户名和密码默认root)

通过网页进行管理dubbo服务 :

参考文献链接 

上述资源代码类下载地址

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值