dubbo服务提供者与消费者简单实例

1、下载一个zookeeper作为注册中心:

(1)在conf文件夹中更改为zoo.cfg的一个zookeeper的配置文件。否则启动zookeeper时报错。zookeeper相关配置在此文件中配置。例如端口号clientport

(2)双击bin文件夹中的zkService.cmd启动zookeeper注册中心。(启动其他之前必须启动注册中心将其他服务注册到注册中心同一管理)

2、下载一个新的tomcat并将webapps\ROOT中内容清空,并将dubbo-admin-2.6.0.war解压到此目录中。启动此tomcat作为zookeeper的控制台。

3、创建一个名称为dubbo-demo-api的mavenproject。此项目为了抽取各个服务的服务接口:

DemoProviderService.java

/**
 * 服务提供者接口
 * @author Administrator
 *
 */
public interface DemoProviderService {

	public String sayHello(String name);
}

4、创建一个名称为dubbo-demo-parent的mavaproject,此项目类型配pom,为了统一项目的jar包管理。

配置pom.xml:

 
  <!-- 依赖版本管理  -->
  <properties>
  	<dubbo-demo-api.version>0.0.1-SNAPSHOT</dubbo-demo-api.version>
  	<dubbo.version>2.6.0</dubbo.version>
    <zkclient.version>0.10</zkclient.version>
    <curator-framework.version>4.0.1</curator-framework.version>
    <fastjson.version>1.2.46</fastjson.version>
    <log4j.version>1.2.17</log4j.version>
    <slf4j-api.version>1.7.25</slf4j-api.version>
    <commons-lang3.version>3.4</commons-lang3.version>
    <netty-all.version>4.0.35.Final</netty-all.version>
  </properties>
  
  
  <!-- 依赖管理 仅仅定义 -->
  <dependencyManagement>
  	<dependencies>
	     <dependency>
              <groupId>com.java1234</groupId>
              <artifactId>dubbo-demo-api</artifactId>
              <version>${dubbo-demo-api.version}</version>
           </dependency>
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>dubbo</artifactId>
              <version>${dubbo.version}</version>
          </dependency>
          <dependency>
              <groupId>com.101tec</groupId>
              <artifactId>zkclient</artifactId>
              <version>${zkclient.version}</version>
          </dependency>
          <dependency>
              <groupId>org.apache.curator</groupId>
              <artifactId>curator-framework</artifactId>
              <version>${curator-framework.version}</version>
          </dependency>
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>fastjson</artifactId>
              <version>${fastjson.version}</version>
          </dependency>
          <dependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>${log4j.version}</version>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>${slf4j-api.version}</version>
          </dependency>
          <dependency>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-lang3</artifactId>
              <version>${commons-lang3.version}</version>
          </dependency>
          <dependency>
              <groupId>io.netty</groupId>
              <artifactId>netty-all</artifactId>
              <version>${netty-all.version}</version>
          </dependency>
	</dependencies>
  </dependencyManagement>
  

5、创建一个名称为dubbo-demo-provider的mavenproject,作为项目的服务提供者。

(1)配置pom.xml文件:

首先加上parent配置作为jar包的版本管理:

<modelVersion>4.0.0</modelVersion>
  <parent>
  	<groupId>com.java1234</groupId>
  	<artifactId>dubbo-demo-parent</artifactId>
  	<version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dubbo-demo-provider</artifactId>

然后引入依赖jar包:

  <dependencies>
  
              <!-- 抽取的公共的服务接口 -->
  		<dependency>
  			<groupId>com.java1234</groupId>
			<artifactId>dubbo-demo-api</artifactId>
  		</dependency>
  
	    <dependency>
	        <groupId>com.alibaba</groupId>
	        <artifactId>dubbo</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>com.101tec</groupId>
	        <artifactId>zkclient</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.apache.curator</groupId>
	        <artifactId>curator-framework</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>com.alibaba</groupId>
	        <artifactId>fastjson</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>log4j</groupId>
	        <artifactId>log4j</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.slf4j</groupId>
	        <artifactId>slf4j-api</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>org.apache.commons</groupId>
	        <artifactId>commons-lang3</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>io.netty</groupId>
	        <artifactId>netty-all</artifactId>
	    </dependency>
	</dependencies>

(2)创建服务提供者接口实现类DemoProviderServiceImpl.java:

在pom.xml已引入抽取的接口,所以这里直接实现就可以

/**
 * 服务提供者接口实现类
 * @author Administrator
 *
 */
public class DemoProviderServiceImpl implements DemoProviderService{

	public String sayHello(String name) {
		return "服务001"+name;
	}

}

(3)在resources中创建配置文件dubbo-demo-provider.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

   <!-- 提供方应用名称,用于计算依赖关系 -->
    <dubbo:application name="demo-provider" />
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <!-- 使用dubbo协议,在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <!-- service实现类作为本地的一个bean -->
    <bean id="demoProviderService" class="com.service.impl.DemoProviderServiceImpl"/>
    
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.service.DemoProviderService" ref="demoProviderService"/>

</beans>

(4)在test中创建启动类ProviderTest.java:

public class ProviderTest {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-provider.xml"});
        context.start();
        System.out.println("服务提供者向zookeeper注册中心注册服务成功(端口:20880)");
        try {
			System.in.read();//保持启动
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // press any key to exit
        context.close();
	}
}
6、创建名称为dubbo-demo-consumer的mavenproject作为服务消费者

(1)配置pom.xml文件,引入jar包和公共接口:

<parent>
  	<groupId></groupId>
  	<artifactId>dubbo-demo-parent</artifactId>
  	<version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>dubbo-demo-consumer</artifactId>
  
  <dependencies>
  
  	<dependency>
  		<groupId>com.java1234</groupId>
	    <artifactId>dubbo-demo-api</artifactId>
  	</dependency>
  
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
	        <artifactId>netty-all</artifactId>
	    </dependency>
	</dependencies>

(2)在resources中创建消费者的配置文件dubbo-demo-consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名称,用于计算依赖关系,不是匹配条件,不要与提供方名称一样  -->
    <dubbo:application name="demo-consumer"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 生成远程服务代理,可以与本地bean一样使用  check属性,启动时候是否检查 一般设置成false 启动时候不检查 -->
    <dubbo:reference id="demoProviderService" check="false" interface="com.java1234.service.DemoProviderService"/>

</beans>

(3)在test中创建启动类ConsumerTest.java

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerTest {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-consumer.xml"});
        context.start();
        DemoProviderService demoProviderService=(DemoProviderService) context.getBean("demoProviderService");
        String result=demoProviderService.sayHello("你好");
        System.out.println("远程调用的结果:"+result);
        try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        context.close();
	}
}

7、dubbo服务集群以及负载均衡:

网页输出http://localhost:8080/进入dubbbo控制台默认账号密码为root/root

在控制台中对服务提供者进行权重调节等工作












展开阅读全文

没有更多推荐了,返回首页