单一应用架构:就是之前练习的系统,将所有的功能都部署在一起,只需一个应用
当网站流量很小时,应用规模小时,只需一个应用,将所有功能都部署在一起,以减少部署服务器数量和成本
适合小型系统,小型网站,或者企业的内部系统,用户较少,请求量不 大,对请求的处理时间没有太高的要求
缺点: 1、性能扩展比较困难 2、不利于多人同时开发 3、不利于升级维护 4、整个系统的空间占用比较大
将核心业务抽取出来,作为独立的服务
分布式系统中的计算机可以使用不同的操作系统,可以运行不同应用程序提供服务,将 服务分散部署到多个计算机服务器上
RPC 【Remote Procedure Call】是指远程过程调用,是一种进程间通信方式,是一种技 术思想,而不是规范
它允许程序调用另一个地址空间(网络的另一台机器上)的过程或函 数,而不用开发人员显式编码这个调用的细节
RPC 的特点
rpc 通讯是基于 tcp 或 udp 议
序列化方式(xml/json/二进制)
一般服务器与服务器之间使用的是长连接方式,用户与服务器之间使用短连接方式访问
面向接口代理:
调用接口的方法,在 A 服务器调用 B 服务器的方法,由 dubbo 实现对 B 的 调用,无需关心实现的细节,就像 MyBatis 访问 Dao 的接口,可以操作数据库一样。不用关心 Dao 接口方法的实现
动态代理对象
支持多种协议:dubbo , hessian , rmi , http, webservice , thrift , memcached , redis。 dubbo 官方推荐使用 dubbo 协议
dubbo 协议默认端口 20880
dubbo 协议采用单一长连接和异步通讯,适用于小数据量大并发的服务调用,不适合于大数据量的服务(比如文件、视频等)
一般服务器与服务器之间使用的是长连接方式,用户与服务器之间使用短连接方式访问
使用 dubbo 协议,spring 配置文件加入:
<dubbo:protocol name="dubbo" port="20880"/>
服务提供者(Provider):暴露服务的服务提供方,服务提供者在启动时,向注册中心注 册自己提供的服务
服务消费者(Consumer): 调用远程服务的服务消费方,服务消费者在启动时,向注册 中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一 台提供者进行调用,如果调用失败,再选另一台调用
注册中心(Registry):注册中心返回服务提供者地址列表给消费者,如果有变更,注册 中心将基于长连接推送变更数据给消费者
监控中心(Monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心(可以没有)
调用关系说明:
先写的简单一点,使用 点对点的直连方式
点对点的直连项目:消费者直接访问服务提供者,没有注册中心
消费者必须指定服务提供者的访问地址(url),消费者直接通过 url 地址访问固定的服务提供者,这个 url 地址是不变的
步骤:
新建 maven web 工程
创建实体类,保存在网络中传输的数据
注意此类需要实现序列化接口,否则后面启动项目会报错
定义服务接口和实现类:UserService
定义 spring 配置文件
测试配置文件是否有效
添加监听器(创建spring容器对象)
将项目打包成 jar 包
<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.0modelVersion>
<groupId>com.afeigroupId>
<artifactId>001_testartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8080port>
<path>/path>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
project>
自定义类,使用 idea 自动生成序列化版本号
在类名上 Alt + Enter ,生成序列化版本号
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private Integer id;
private String username;
private Integer age;
}
public interface UserService {
User queryUserById(Integer id);
}
public class UserServiceImpl implements UserService {
@Override
public User queryUserById(Integer id) {
User user = new User(1001, "lisi", 20);
return user;
}
}
<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="001-link-userservice-provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.service.UserService" ref="userServiceImpl" registry="N/A"/>
<bean id="userServiceImpl" class="com.service.impl.UserServiceImpl"/>
beans>
第二、三步可以合并为一个标签
<dubbo:service interface="com.service.UserService"
ref="userServiceImpl"
protocol="dubbo"
registry="N/A"/>
写完配置文件可以进行简单测试,如果没有报错,验证读取是否成功、配置是否有效
public static void main(String[] args) {
//创建spring容器对象
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-userservice-provider.xml");
ctx.start();
}
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:dubbo-userservice-provider.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
步骤:
消费者怎么知道提供者暴露了哪些接口呢?
这里需要将提供者的工程打包成一个 jar 包,然后放入本地仓库,作为消费者 maven 的一个依赖
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.2version>
dependency>
<dependency>
<groupId>com.afeigroupId>
<artifactId>001_testartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="002-link-consumer"/>
<dubbo:reference interface="com.service.UserService"
id="remoteUserService"
url="dubbo://localhost:20880" registry="N/A"/>
<bean id="invokeService" class="com.service.InvokeService">
<property name="service" ref="remoteUserService"/>
bean>
beans>
public class MyClient {
public static void main(String[] args) {
//创建 spring 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
//从容器中获取远程的代理对象
UserService service = (UserService) ctx.getBean("remoteUserService");
System.out.println(service); //com.alibaba.dubbo.common.bytecode.proxy0@776aec5c 说明是生成的代理对象
//通过代理对象执行方法的调用,就像调用自己的方法一样
User user = service.queryUserById(1001);
System.out.println("通过代理对象执行方法的调用结果" + user);
}
}
需要启动服务提供者的 Tomcat 服务,然后执行 消费者中使用者类的 main 方法
当执行 service.queryUserById(1001);
,方法传入参数之后,进行序列化,传递到 001提供者 项目中
该项目执行得到 user 对象,然后经过序列化传递回 002 消费者项目
002经过反序列化得到结果 user 对象,赋值给 User user
(也可以创建 springmvc 配置文件,使用注解方式,定义controller类,来调用业务类对象执行方法)
定义业务类
@Setter
public class InvokeService {
private UserService service;
public void printService(){
User user = service.queryUserById(1001);
System.out.println("通过代理对象执行方法的调用结果" + user);
}
配置文件中声明 bean 对象
<bean id="invokeService" class="com.service.InvokeService">
<property name="service" ref="remoteUserService"/>
bean>
使用者类创建业务类对象,执行方法
public class MyClient {
public static void main(String[] args) {
//创建 spring 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
//从容器中获取 Service 对象
InvokeService service = (InvokeService) ctx.getBean("invokeService");
service.printService();
}
}
需要启动服务提供者项目的 Tomcat 服务,然后执行消费者项目中使用者类的 main 方法
假如此时有多个功能提供者,消费者需要使用查看用户信息、获取天气状况、查看电影信息三个功能
因为消费者项目中需要:
此处多个功能就有多个jar包,麻烦,所以规范化应该是如下结构:
Dubbo 中常用标签。分为三个类别:公用标签,服务提供者标签,服务消费者标签
<dubbo:application/> 和 <dubbo:registry/>
配置应用信息
<dubbo:registry address="ip:port" protocol="协议"/>
配置访问服务提供者的协议信息
<dubbo:protocol name="dubbo" port="20880"/>
配置暴露的服务
<dubbo:service interface="服务接口名" ref="服务实现对象 bean"/>
配置服务消费者引用远程服务
<dubbo:reference id="服务引用 bean 的 id" interface="服务接口名"/>
服务启动时,会检查相关的依赖是否可用,如不可用则会抛出异常(比如此处必须先运行提供者的 Tomcat,否则报错)
关闭这个启动时检查:(比如先运行消费者,再运行的提供者,就需要关闭检查)
<dubbo:reference interface="com.service.UserService" check="false"/>
关闭注册中心启动时检查:
<dubbo:registry check="false"/>
远程服务调用重试次数,不包括第一次调用,默认是 2 次,加上第一次共 3 次
<dubbo:reference retries="5" />
现在在消费者项目的配置文件中,对于 引用远程服务接口 中的访问地址,是写死的,现在使用注册中心可以使其动态化,防止出现链接崩坏而导致的项目问题
服务注册中心可以通过特定协议来完成服务对外的统一
Dubbo 提供 的注册中心有如下几种类型可供选:
Zookeeper 是一个高性能的,分布式的,开放源码的分布式应用程序协调服务,简称 zk
Zookeeper 是翻译管理是动物管理员,可以理解为 windows 中的资源管理器或者注册表
他是一个树形结构,这种树形结构和标准文件系统相似,ZooKeeper 树中的每个节点被称为 Znode,和文件系统的目录树一样,ZooKeeper 树中的每个节点可以拥有子节点,每个节点表 示一个唯一服务资源
Zookeeper 运行需要 java 环境
下载的文件 zookeeper-3.5.4-beta.tar.gz 解压后到目录就可以了,例如 d:/servers/ zookeeper-3.5.4
修改 zookeeper-3.5.4/conf/ 目录下配置文件
复制一份 zoo-sample.cfg ,并改名为 zoo.cfg
文件内容:
tickTime:心跳的时间,单位毫秒
Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳,表明存活状态
dataDir:数据目录,可以是任意目录
存储 zookeeper 的快照文件、pid 文件,默认为 /tmp/zookeeper,建议在 zookeeper 安装目录下创建 data 目录,将 dataDir 配置改为 /usr/local/zookeeper-3.4.10/data
clientPort:客户端连接 zookeeper 的端口,即 zookeeper 对外的服务端口,默认为 2181
配置内容:
dataDir :zookeeper 数据的存放目录
admin.serverPort=8888
原因:zookeeper 3.5.x 占用 8080
启动:
打开解压目录的 bin 目录,双击 zkServer.cmd ,启动服务器
zkCli.cmd 启动命令客户端
Zookeeper 的运行需要 jdk。使用前 Linux 系统要安装好 jdk
①:上传 zookeeper-3.5.4-beta.tar.gz 并解压
解压文件执行命令:tar -zxvf zookeeper-3.5.4-beta.tar.gz -C /usr/local/
②:配置文件
在 zookeeper 的 conf 目录下,拷贝样例文件 zoo-sample.cfg 为 zoo.cfg,cp zoo_sample.cfg zoo.cfg
zookeeper 启动时会读取该文件作为默认配置文件
③:启动 Zookeeper
启动(切换到安装目录的 bin 目录下):./zkServer.sh start
④:关闭 Zookeeper
关闭(切换到安装目录的 bin 目录下):./zkServer.sh stop
将服务提供者注册到注册中心(暴露服务)
让服务消费者去注册中心订阅服务提供者的服务地址
消费者需要引入和提供者一样的依赖
配置文件
仅提供实体类、需要暴露的公共接口
<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.0modelVersion>
<groupId>com.afeigroupId>
<artifactId>dubbo-public-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
project>
注意实体类要实现序列化接口
@Data
public class User implements Serializable {
private Integer id;
private String username;
private Integer age;
}
public interface UserService {
User queryUser(Integer id);
}
需要引入公共接口项目打包为 jar 包,放入maven本地仓库作为依赖
<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.0modelVersion>
<groupId>com.afeigroupId>
<artifactId>user-providerartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.2version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>com.afeigroupId>
<artifactId>dubbo-public-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
project>
public class UserServiceImpl implements UserService {
@Override
public User queryUser(Integer id) {
User user = new User();
user.setId(id);
user.setUsername("lisi");
user.setAge(id + 1);
return user;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="user-provider"/>
<dubbo:registry address="zookeeper://192.168.162.128:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.service.UserService" ref="userServiceImpl"/>
<bean id="userServiceImpl" class="com.serviceImpl.UserServiceImpl"/>
beans>
对应着xml配置的 <dubbo:service> 和
,注解是 @DubboService 和 @DubboReference
注:2.7.7 开始才增加这两个注解,之前使用的是@Service和@Reference
(从 8 到 13)需要提供的是对应的 spring bean 的名字
注意:
关闭 Linux 防火墙、启动ZooKeeper服务、启动监控中心(运行 jar 包并访问 7001 端口)
第一种方式:在上面配置的基础上,仅需要创建一个类,来加载 spring 配置生成容器对象,并执行 start() 方法
public class MainApplication {
//必须保证此方法在运行,没有停止,注册中心中才可以有提供者存在
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext appc = new ClassPathXmlApplicationContext("provider.xml");
appc.start();
System.in.read(); //此句不可省
}
}
注意:
关闭 Linux 防火墙、启动ZooKeeper服务、启动监控中心(运行 jar 包并访问 7001 端口)
第二种方式:在上面配置的基础上
配置 web.xml 方式,配置监听器,来生成 Spring 容器对象
配置 pom.xml 文件,添加 Tomcat 插件,启动 tomcat来启动 提供者服务
注意更改端口号,将打包方式改成 war
配置 spring 容器监听器
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:provider.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<packaging>warpackaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8081port>
<path>/path>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
需要引入公共接口项目打包为 jar 包,放入maven本地仓库作为依赖
文件内容与提供者的内容一致
<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.0modelVersion>
<groupId>com.afeigroupId>
<artifactId>consumerartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.16.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.6.2version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>com.afeigroupId>
<artifactId>dubbo-public-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8080port>
<path>/path>
<uriEncoding>UTF-8uriEncoding>
configuration>
plugin>
plugins>
build>
project>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://192.168.162.128:2181"/>
<dubbo:reference interface="com.service.UserService" id="remoteUserService"/>
beans>
对应着xml配置的 <dubbo:service> 和
,注解是 @DubboService 和 @DubboReference
注:2.7.7开始才增加这两个注解,之前使用的是@Service和@Reference
(从 7 到 12)需要提供的是对应的 spring bean 的名字
启动类就要使用 @EnableDubbo: 开启注解Dubbo功能 ,其中可以加入scanBasePackages属性配置包扫描的路径,用于扫描并注册bean
通过 @EnableDubbo
可以在指定的包名下(通过 scanBasePackages
),或者指定的类中(通过 scanBasePackageClasses
)扫描 Dubbo 的服务提供者(以 @Service
标注)以及 Dubbo 的服务消费者(以 Reference
标注)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.service"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xml,classpath:consumer.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
@Component
public class ConsumerService {
@Resource
private UserService userService;
public void printTest(Integer userid){
User user = userService.queryUser(userid);
System.out.println(user);
}
}
@Controller
public class MyController {
@Resource
private ConsumerService service;
@RequestMapping("/test")
public String tt(Integer id){
service.printTest(id);
return "hello";
}
}
dubbo 的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入 dubbo-admin
通过 dubbo-admin 可以对消费者和提供者进行管理。可以在 dubbo 应用部署做动态的调整, 服务的管理
到官网的 GitHub页面,拉到最下面,点击 dubbo admin
这里下载的是源代码,需要手工编译才能使用
下载
修改 src\main\resources\application.properties 指定zookeeper地址
如果启动的 ZooKeeper 是 Windows 版本的,地址就如下图:
如果启动的 ZooKeeper 是 Linux 版本的,地址就填虚拟机地址:
进入 pom.xml 文件所在的目录。在目录栏使用 cmd ,打开命令窗口
执行命令: mvn clean package
清理并打包
将jar包挪出来,然后在 jar 包所在目录位置 cmd
注意:
执行命令:java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
启动后不关闭窗口,在浏览器访问端口 7001
默认使用root/root 登陆
负载均衡是以集群为前提,将工作任务进行平衡、分摊到多个操作单元上进行执行
负载均衡有两个方面的含义:
随机,按权重设置随机概率
轮循,按公约后的权重设置轮循比率
存在慢的提供者累计请求问题,服务器性能相近时使用
最少活跃调用书,系统活跃数的随机,活跃数指调用前后计数差
服务器性能相差较大时使用
一致性 Hash,相同参数的请求总是发到同一提供者,当某一提供者挂是,将需求会平摊到其他提供者
对故障的处理波动较小
需要是集群环境中实现
配置方式:
<dubbo:reference interface="..." loadbalance="roundrobin"/>
或
<dubbo:service interface="..." loadbalance="roundrobin"/>