一:集群和分布式
集群:一个业务模块,部署在多台服务器上
分布式:一个大的业务系统,拆分为小的业务模块,分别部署在不同的机器上
二:dubbo入门
1.概念:阿里巴巴基于java形成的一种高性能RPC(一种运行调用机制)的分布式服务架构。是SOA架构的产物。
2.运行环境:安装zookeeper注册中心下进行。
3.入门案例:
普通的spring与springmvc的整合,web依赖于service(在一台电脑上开发时)。
采用dubbo架构后,需要将service与web部署在不同的服务器上,web通过rpc机制去调用service。
首先修改service
3.1将接口实现类的@Service注解改为dubbo包下的
package com.dubbo.service.impl;
import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
//@Service//将该类注册到spring容器中
@Service//将该类对外发布,将访问方法的ip,端口,路径放在注册中心中
public class UserServiceImpl implements UserService {
public String sayHello(){
return "hello dubbo!";
}
}
3.2将service改为war包形式,改为wep-app项目,在pom.xml文件中修改,导入webapp包
3.3web.xml文件配置
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.4配置spring配置文件
<?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://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://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--扫描service包-->
<context:component-scan base-package="com.dubbo.service"></context:component-scan>
<!-- dubbo配置-->
<!-- 1.配置项目名称(唯一)-->
<dubbo:application name="dubbo-servie"/>
<!-- 2.配置注册中心-->
<dubbo:registry address="zookeeper:IP地址"/>
<!-- 3.配置dubbo包扫描-->
<dubbo:annotation package="com.dubbo.service.impl"/>
</beans>
3.5将dubbo-service部署在tomcat上s
3.6创建一公告接口模块,service和web依赖于该接口模块,将接口放在改模块中
3.7将interface模块导入service和web的pom.xml文件中,将interface模块install在maven仓库中
其次修改web层
3.8将控制类的@Autowired修改
package com.dubbo.controller;
import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
//@Autowired
/**
* 1.从zookeeper注册中心获取userService访问的url
* 2.进行远程调用rpc
* 3.将结果封装为一个代理对象,给变量赋值
*/
@Reference//远程注入
private UserService userService;
@RequestMapping("/say")
public String sayHello(){
System.out.println("controller");
return userService.sayHello();
}
}
3.9修改spring配置文件
<?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://dubbo.apache.org/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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-4.3.xsd ">
<mvc:annotation-driven>
<!-- 版本过高的spring需要手动开启后缀请求-->
<mvc:path-matching suffix-pattern="true"/>
</mvc:annotation-driven>
<context:component-scan base-package="com.dubbo.controller"></context:component-scan>
<!-- dubbo配置-->
<!-- 配置项目名称(唯一)-->
<dubbo:application name="dubbo-web">
<!-- 该端口(因为我们是在一台电脑上测试,默认端口会冲突)-->
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<!-- 配置注册中心-->
<dubbo:registry address="zookeeper:IP地址"/>
<!-- 配置dubbo包扫描-->
<dubbo:annotation package="com.dubbo.controller"/>
</beans>
3.10修改web.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- spring -->
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>-->
<!-- springmvc-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.11将web部署在tomcat即可
总体架构
三:dubbo高级特性
1:序列化
在dubbo中实现对象的传递需要采用序列化(将对象变为字节序列)与反序列化(将字节序列变为对象),保证数据的完整性和可传递性。
具体过程
2.地址缓存
当注册中心挂掉后,消费者是否可以调用方法? 可以的,消费者第一次访问注册中心会将服务 提供方地址缓存在本地,再次访问无需访问注册中心
3.超时
提供者和消费者都可以设置timeout
设置timeout后,在这个时间段内无法访问服务,自动断开
提供者
消费者
消费者时间会替代提供者 建议在service建立超时设置。
4.重试–处理网络抖动(超时后才会重试)
提供方设置 retries默认为2
消费方设置 retries默认为2
5.多版本
定义接口版本
根据版本调用接口
6.负载均衡(在集群环境上)
random
2被访问的概ro率是50%,1.3分别为25%。
roundrobin
leastactive
consistenhash
在@Reference中设置
7.集群容错
8.服务降级