Dubbo框架使用demo

Dubbo框架使用demo

Dubbo调用关系说明

  • 服务容器负责启动,加载,运行服务提供者。
  • 服务提供者在启动时,向注册中心注册自己提供的服务。
  • 服务消费者在启动时,向注册中心订阅自己所需的服务。
  • 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  • 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

1、安装zookeeper

zookeeper下载下来后修改conf文件里的文件名称,有可能会报找不到JAVA_HOME的错误,可以参考该文章,修改 zkEvn  中的Java安装路径 。

https://www.cnblogs.com/china-baizhuangli/p/8920776.html

CMD启动命令:D:\zookeeper\apache-zookeeper-3.5.5-bin\bin>zkServer.cmd 看到这一行就表示启动ok:

2019-08-06 15:43:04,688 [myid:] – INFO [main:NIOServerCnxnFactory@686] – binding to port 0.0.0.0/0.0.0.0:2181

2、dubbo-demo-api

DemoService.java
package com.aotian.demo.dubbo.api;

/**
 * 测试接口类.
 * 定义服务类的接口.
 * @author aotian
 *
 */
public interface DemoService {

	/**
	 * 测试方法,在用户名后添加一个字符串
	 * @param username	用户名
	 * @return	修改后的字符串
	 */
	String changeUsername(String username);
	
}

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>
<parent>
<groupId>com.aotian</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-api</artifactId>
</project>

2、dubbo-demo-provider

DemoServiceImpl.java 接口实现类
package com.aotian.demo.dubbo.provider;

import com.aotian.demo.dubbo.api.DemoService;

/**
 * 测试服务端接口实现类
 * @author aotian
 *
 */
public class DemoServiceImpl implements DemoService{

	/**
	 * 测试方法,在用户名后添加一个字符串
	 * @param username	用户名
	 * @return	修改后的字符串
	 */
	@Override
	public String changeUsername(String username) {
		return username + " hello world!";
	}

}
Provider.java类 
package com.aotian.demo.dubbo.provider;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
context.start();
System.in.read();
}

}

dubbo-provider.xml 在Provider 类中被加载

<?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应用程序命名-->
<dubbo:application name="dubbo-demo-provider"/>

<!--dubbo注册地址-->
<dubbo:registry address="zookeeper://localhost:2181"/>

<!--dubbo协议地址-->
<dubbo:protocol name="dubbo" port="20880"/>

<!--接口声明-->
<dubbo:service interface="com.aotian.demo.dubbo.api.DemoService" ref="demoService"/>
<bean id="demoService" class="com.aotian.demo.dubbo.provider.DemoServiceImpl"/>
</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>
<parent>
<groupId>com.aotian</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>

<dependencies>
<dependency>
<groupId>com.aotian</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

3、dubbo-demo-consumer

Consumer类 
package com.aotian.demo.dubbo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aotian.demo.dubbo.api.DemoService;

public class Consumer {

public static void main(String[] args) {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();

String username = "tianhl";
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.changeUsername(username));
}

}

dubbo-consumer.xml在Consumer类中被加载

<?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应用程序命名-->
    <dubbo:application name="dubbo-demo-consumer"/>

    <!--dubbo注册地址-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--接口引用-->
    <dubbo:reference interface="com.aotian.demo.dubbo.api.DemoService" id="demoService"/>
</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>
	<parent>
		<groupId>com.aotian</groupId>
		<artifactId>dubbo-demo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>dubbo-consumer</artifactId>
	
	<dependencies>
		<dependency>
			<groupId>com.aotian</groupId>
			<artifactId>dubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

整个工程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.aotian</groupId>
	<artifactId>dubbo-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	
	<modules>
		<module>dubbo-provider</module>
		<module>dubbo-consumer</module>
		<module>dubbo-api</module>
	</modules>
	
	<dependencies>
		<!-- dubbo当前最新版 -->
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- zookeeper客户端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
	</dependencies>
	
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

先启动zk,然后启动provider,然后再启动consumer看效果!

发表评论

电子邮件地址不会被公开。 必填项已用*标注