赞
踩
项目使用zookeeper作为注册中心通过springboot整合dubbo,完成远程调用,做到对dubbo作用的入门,对它的作用和配置大概的了解
dubbo的演变和作用:最开始的是将项目中所有代码写在一个类中,定位各模块代码时十分麻烦,所以演变为后来的MVC模式,将视图层、业务层、数据库访问层分开,定位清晰。但是这种架构无法解决高并发项目的需求,因为项目都安装在一台计算机上,当访问量大时一台电脑无法满足需求,所以有了rpc远程调用。将前端界面和服务器安装在多台计算机上,服务器计算机有多个端口和多个服务(例如8881学生管理服务,8882图书服务,8883注册服务),这种模式提高了计算机的抗压能力。但当服务器上的端口改变时前端服务调用的地方也需要修改不利于维护,特别是业务复杂时。故有了后面的SOA即面向服务的架构,dubbo就是完美的实践者。实际就是在服务调用和服务提供中间增加一个注册中心,发布服务只需向注册中心注册即可,注册中心记住端口号和对应服务的关系

项目包和文件图:

需要注意的点:
<?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 服务提供者xml配置文件 --> <!-- 应用配置,不要与提供方相同,配置dubbo的应用名称 --> <dubbo:application name="students-server" version="2.0"/> <!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.0.173:2181" timeout="60000"/> <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。--> <dubbo:consumer check="false"/> <!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface--> <dubbo:annotation package="com.luntek.studentsserver.server.impl"/> <!-- <dubbo:service interface="com.luntek.studentsserver.server.StudentServer" ref="studentServerImpl"/>--> <context:component-scan base-package="com.luntek.studentsserver.server.impl"></context:component-scan> </beans>
package com.luntek.studentsserver.server.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.luntek.studentsserver.server.StudentServer;
@Service //阿里巴巴提供的service注解
public class StudentServerImpl implements StudentServer {
//dubbo提供的具体服务
@Override
public String server(String name) {
return "server:"+name;
}
}
<dependencies> <!--Web支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.10</version> </dependency> <!--<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>--> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </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> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.21.0-GA</version> </dependency> </dependencies>

# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
项目目录结构截图:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <!--Web支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.10</version> </dependency> <!-- <dependency>--> <!-- <groupId>com.alibaba.spring.boot</groupId>--> <!-- <artifactId>dubbo-spring-boot-starter</artifactId>--> <!-- <version>2.0.0</version>--> <!-- </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> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.21.0-GA</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.8.RELEASE</version> <scope>compile</scope> </dependency> </dependencies>
<?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"> <!--http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd--> <!-- 应用配置,不要与提供方相同,配置dubbo的应用名称 --> <dubbo:application name="students-consumer" version="2.0"/> <!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.0.173:2181" timeout="60000"/> <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。--> <dubbo:consumer check="false"/> <!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface--> <dubbo:annotation package="com.luntek.controller"/> <!-- <dubbo:reference id="studentServerImpl" check="false" interface="com.luntek.server.StudentServer" />--> </beans>
package com.luntek.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.luntek.studentsserver.server.StudentServer; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * 客户端的web层,目的是远程调用服务端的StudentServer的server方法 */ @Controller @RequestMapping("controller") public class StudentController { @Reference //将远程的studentServer注入到该属性 private StudentServer server; //请求服务端代码 @ResponseBody @RequestMapping("/hello") public String rpcServer(){ System.err.println("rpcServer调用方法"); return server.server("czw"); } }

将服务端和客户端配置完成之后先将项目studentsserver启动,再启动student-client项目,无异常的话就没问题
注:消费端声明的接口必须和服务端的接口的全限定名相同否则会有RpcException: No provider available from registry异常
项目启动完成后访问controller中的接口 http://localhost:8459/controller/hello 成功获取调取,消费端打印信息


Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。