Dubbo--HelloWorld

本文介绍如何利用Dubbo技术栈实现经典HelloWorld示例,包括项目目录结构、消费者端和服务提供者端代码,以及使用multicast方式的注册中心配置。通过实例演示了如何将服务提供者与消费者解耦,简化分布式服务的管理和调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       上篇博客对Dubbo进行了大概的介绍,这篇博客介绍如何使用Dubbo,经典的HelloWorld

     通过上篇博客我们知道,在dubbo中有一个很重要的角色是注册中心,Dubbo现在支持的有三种方式:1.multicast2.zookeeper3.redis。下面的Demo使用的是multicast方式。

项目目录结构:

消费者:


服务者:


 

消费者端代码:

ConsumerThd.java

package com.tgb.klx.dubbo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tgb.klx.dubbo.server.HelloWorld;
public class ConsumerThd implements Runnable {
	public void run() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "applicationConsumer.xml" });
		context.start();
		HelloWorld helloWorld = (HelloWorld) context.getBean("demoService");
		String hello = helloWorld.hello("小康");
		System.out.println(hello);
	}
	public static void main(String[] args) {
		new Thread(new ConsumerThd()).start();
	}
}


applicationConsumer.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="consumer-of-helloworld-app" />
  <dubbo:registry address="multicast://224.5.6.7:1234"/>
  <dubbo:reference id="demoService" interface="com.tgb.klx.dubbo.server.HelloWorld"/>                                
</beans> 


Pom.xml

<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.tgb.klx</groupId>
<artifactId>dubbo-Consumer-multicast</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-Consumer</name>
 
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
    </dependency>
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
    </dependency>
    <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.15.0-GA</version>
    </dependency>
    <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
    </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
    <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
    </dependency>
    <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
    </dependency>
    <dependency>
            <groupId>com.github.adyliu</groupId>
            <artifactId>zkclient</artifactId>
            <version>2.0</version>
    </dependency>
    <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.6</version>
    </dependency>
  </dependencies>
</project>


 

服务端,即服务提供者端代码:

HelloWorld.java

package com.tgb.klx.dubbo.server;

public interface HelloWorld {

	public String hello(String name);
}


 

HelloWorldImpl.java

 

package com.tgb.klx.dubbo.server;

public class HelloWorldImpl implements HelloWorld{

	public String hello(String name) {
		name=name+"小康测试";
		return name;
	}

}

DubboProviderMain.java

package com.tgb.klx.dubbo.server;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboProviderMain {

	public static void main(String[] args) throws IOException{
		
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(
				new String[]{"applicationProvider.xml"});
		context.start();
		 System.out.println("Press any key to exit.");  
	     System.in.read();  
	}
}

 

applicationProvider.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="hello-world" /><!-- 注册地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<dubbo:protocol name="dubbo" port="20880" />        
<dubbo:service interface="com.tgb.klx.dubbo.server.HelloWorld" ref="demoService" executes="10" />
<bean id="demoService" class="com.tgb.klx.dubbo.server.HelloWorldImpl" />
</beans>


Pom.xml

<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.tgb.klx</groupId>
  <artifactId>dubbo-Server-multicast</artifactId>
  <version>0.0.1-SNAPSHOT</version>
          <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.15.0-GA</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
 
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>com.github.adyliu</groupId>
<artifactId>zkclient</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.0.Final</version>
</dependency>
</dependencies>
</project>


运行结果:


 

       还有一种方式是将Zookeeper作为注册中心,需要下载并安装zookeeper。将配置文件中的注册中心修改为如下即可:


       其他代码不用修改,推荐使用zookeeper作为注册中心。如此可以通过Dubbo+注册中心实现分布式服务。服务消费者不用知道服务的具体位置,只要知道注册中心位置即可,消费者只要能消费,不用管消费的是哪的服务,服务提供方与服务消费方解耦。

       使用zookeeper注册中心,与dubbo结合,可以查看服务提供者和消费者的信息,将dubbo管理的war包部署到tomcat上,查看服务提供者,如下图:

部署Dubbo管理的war包到tomcat上,即可查看服务提供者与消费者信息。如下图:提供者:


 

服务消费者,如下图:


 

注:dubbo-admin.war部署在jdk1.8上会报错,使用1.7没有问题。

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值