<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.itheimagroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
package com.xiao.controller;
import com.xiao.pojo.User;
import com.xiao.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
// @Autowired 在统一模块中,可以使用spring自动注入,但是现在在不同模块中,并且部署在不同电脑上。
// 那么需要dubbo给我们进行注入 org.apache.dubbo.config.annotation.Reference;
@Reference(timeout = 1000,retries = 2)
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
@RequestMapping("/find")
public User find(int id){
return userService.findUserByid(id);
}
}
springmvc.xml配置文件
<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.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 https://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.xiao.controller"/>
<dubbo:application name="dobbo-web">
<dubbo:parameter key="qos.port" value="33333"/>
dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:annotation package="com.xiao.controller"/>
beans>
web.xml配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
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.0modelVersion>
<groupId>com.xiaogroupId>
<artifactId>dobbo-webartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<spring.version>5.1.9.RELEASEspring.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.xiaogroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8000port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
package com.xiao.service.impl;
import com.xiao.pojo.User;
import com.xiao.service.UserService;
import org.apache.dubbo.config.annotation.Service;
@Service(timeout = 5000,retries = 2)
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "hello,dubbo";
}
@Override
public User findUserByid(int id) {
User xiao = new User(1, "xiao", "123");
return xiao;
}
}
applicationContext.xml
<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 https://www.springframework.org/schema/context/spring-context.xsd">
<dubbo:protocol port="20880" />
<dubbo:application name="dobbo-service"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:annotation package="com.xiao.service"/>
beans>
web.xml 因为这个模块需要对外提供服务 所以需要运行所需的web.xml文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
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.0modelVersion>
<groupId>com.xiaogroupId>
<artifactId>dobbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<spring.version>5.1.9.RELEASEspring.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>com.xiaogroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>9000port>
<path>/path>
configuration>
plugin>
plugins>
build>
project>
消费者需要和提供者接口进行统一,所以将接口提出来打成jar包,两个模块分别引入对应依赖即可。最后有服务提供模块实现代码
package com.xiao.service;
import com.xiao.pojo.User;
public interface UserService {
String sayHello();
User findUserByid(int id);
}
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.0modelVersion>
<groupId>com.xiaogroupId>
<artifactId>interfaceartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.xiaogroupId>
<artifactId>dobbo-pojoartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
同理,两个需要统一实体类,那么将实体类提出来,打成jar包,在接口模块中进行引入即可
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.0modelVersion>
<groupId>com.xiaogroupId>
<artifactId>dobbo-pojoartifactId>
<version>1.0-SNAPSHOTversion>
project>
User类:
一定要实现Serializable接口,因为服务提供者在返回对象时,通过数据流的形式返回,那么java对象就必须转换成流的形式,服务提供者 对java对象进行序列化,消费者进行反序列化。我们做的只需要实现接口。序列化和反序列化的操作由Dubbo替我们进行实现。
package com.itheima.pojo;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private String pwd;
// 省略构造方法和get set 方法
}
注册中心挂了,服务是否可以正常访问?----可以
因为dubbo服务消费者在第一次调用时,会将服务提供方的地址缓存到本地,以后再一次调用时则不会访问注册中心。
当服务提供者地址发生变化时,注册中心会通知服务消费者更新服务提供者的地址。
@Service(timeout = 5000,retries = 2) // 服务提供者处设置 连接时间超过5s则断开连接(默认为1)。重试次数2 默认为2 在第一次失败之后进行计数 所以设置为2 一共为3
@Reference(timeout = 1000,retries = 2) // 服务消费者处设置
// 两个地方都进行设置 ,则以服务消费方为准
// 一般在服务提供方进行设置
当一个服务有个新版本,但是新版本不i稳定,我们就让小部分服务消费者访问新版本
同一个接口的不同版本完成灰度发布
@Service(version = "v2.0") // 服务提供方
@Service(version = "v1.0") // 服务提供方
@Reference(version = "v2.0") // 服务消费方
负载均衡策略(4种)
Random:按照权重比例,默认值。按照权重设置随机概率
roundrobin:按权重轮询,权重越高,一次轮询调用次数越多
leastactive:最少活跃调用数(最后一次处理请求所花的时间),相同活跃数的随机。
consistenHash:一致性hash,相同的参数请求总是发到同一提供者(比如参数id=1的请求第一次发到了二号机器上,那么后面所有id=1的请求都会发到二号机器上)
@Service(weight=100) // 默认值也是100 weight设置权重
@Reference(loadbalance = "random") // 默认也是random
@Service(weight=100) // 默认值也是100
@Reference(loadbalance = "roundrobin") // 默认也是random
集群容错模式:
Failover Cluster:失败重试。默认值。当出现失败,重试其它服务器,默认重试2次,使用retries配置,一般用于读操作(要是写操作,因为延时导致重试,那么会出现两笔数据)。
Failfast Cluster:快速失败,只发起一次调用,失败立即报错,一般用于写操作。
Failsafe Cluter:失败安全,出现失败直接忽略,返回一个空结果。
Failback Cluster:失败自动恢复,后台记录失败请求,定时重发直到成功为止。
Forking Cluster:并行调用多个服务器,只有一个成功即返回。消耗性能。
Broadcast Cluster:广播调用所有提供者,逐个调用,任意一台报错则报错。
@Reference(cluster = "failover") // failfast,failsafe,failback,forking,broadcast
一台机器有多个服务模块。当机器负荷即将过载,则将相比之下不重要的服务不被调用,比如 : 广告服务,日志服务,支付服务。符合过载时,将广告和日志服务进行降级处理。
服务降级方式:
mock=force:return null 表示消费者对该服务的方法调用直接返回null值,不发起远程调用,用来屏蔽不重要不可用时对调用方的影响。
mock=fail:return null 表示消费方对该服务的方法调用在失败后,再返回null值,不抛异常,用来容忍不重要服务不稳定时对调用方的影响。
@Reference(mock="force:return null") // 不再调用服务