dubbo和zookeeper实现服务远程调用

zookeeper的安装配置参照zookeeper安装配置

dubbo是一种分布式服务框架,解决随着网站应用规模的扩大,服务越来越多,服务间依赖关系变得错综复杂,服务的调用量越来越大,服务的容量问题,dubbo架构图
在这里插入图片描述
节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。

调用关系说明:

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

服务端

  1. 服务端和消费端工程pom文件下引入主要包:
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.4.10</version>
		<exclusions>
			<exclusion>
				<artifactId>spring</artifactId>
				<groupId>org.springframework</groupId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.14</version>
	</dependency>
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>
  1. 服务端配置文件
    src/main/resources/spring/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:application name="hello-world-app"  />  

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

    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  

    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="org.education.academic.service.DemoService" ref="demoService" />  

    <!-- 和本地bean一样实现服务 -->  
    <bean id="demoService" class="org.education.academic.service.impl.DemoServiceImpl" />  

</beans>
  1. 定义接口DemoService.java
package org.education.academic.service;

public interface DemoService {
	String sayHello(String name);
}
  1. 接口实现DemoServiceImpl.java
package org.education.academic.service.impl;

import org.education.academic.service.DemoService;

public class DemoServiceImpl implements DemoService{
	public String sayHello(String name) {  
        return "Hello " + name;  
    } 
}
  1. Spring mvc的web.xml中加载定义的服务端配置文件
  <!-- Spring MVC -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description>SpringMVC</description>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml,classpath:spring/provider.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

消费端

  1. 消费端配置文件
    src/main/resources/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="consumer-of-helloworld-app"  />  

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
    <dubbo:registry address="zookeeper://X.X.X.X:2181"  register="false" />  

   <!-- <dubbo:annotation package="org.education.portal.service"/>  -->
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
    <dubbo:reference id="demoService" interface="org.education.academic.service.DemoService" />

</beans>
  1. 定义消费端接口DemoService.java
package org.education.academic.service;

public interface DemoService {
	String sayHello(String name);
}
  1. 定义消费端服务类DemoService1.java
package org.education.portal.service;

import org.education.academic.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DemoService1 {
//	@Reference
	@Autowired
	DemoService demoService;

    public String say(String name) {

    	String s = demoService.sayHello(name);
    	System.err.println(s);
        return s;
    }
}
  1. 在springboot的启动类中加载消费端的配置文件
@ImportResource("classpath:consumer.xml")
public class XXXApplication extends SpringBootServletInitializer {
	.....
}
  1. 在Controller中注入并使用服务
    @Autowired
    DemoService1 demoService;

	demoService.say("31321321312");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值