一、项目结构
服务提供方:
消费方:
提供者pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.fox</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fox</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Spring Boot Dubbo 依赖-->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
消费者pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.fox</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fox</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Spring Boot Dubbo 依赖-->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
提供者properties
server.port=8081
#dubbo提供者的别名,只是个标识
spring.dubbo.application.name=provider
#zk地址
spring.dubbo.registry.address=zookeeper://172.16.10.15:2181
#dubbo协议
spring.dubbo.protocol.name=dubbo
#duboo端口号
spring.dubbo.protocol.port=20880
#这是你要发布到dubbo的接口所在包位置
spring.dubbo.scan=com.fox
消费者properties
server.port=8080
#dubbo提供者的别名,只是个标识
spring.dubbo.application.name=consumer-1
#zk地址
spring.dubbo.registry.address=zookeeper://172.16.10.15:2181
#dubbo协议
spring.dubbo.protocol.name=dubbo
#duboo端口号
spring.dubbo.protocol.port=20880
#这是你要发布到dubbo的接口所在包位置
spring.dubbo.scan=com.fox
一、Filter使用:
package com.fox.config;
import com.alibaba.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class AuthorityFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorityFilter.class);
private IpWhiteList ipWhiteList;
//dubbo通过setter方式自动注入
public void setIpWhiteList(IpWhiteList ipWhiteList) {
this.ipWhiteList = ipWhiteList;
}
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!ipWhiteList.isEnabled()) {
LOGGER.info("白名单禁用");
return invoker.invoke(invocation);
}
String clientIp = RpcContext.getContext().getRemoteHost();
LOGGER.info("访问ip为{}", clientIp);
List<String> allowedIps = ipWhiteList.getAllowedIps();
if (allowedIps.contains(clientIp)) {
return invoker.invoke(invocation);
} else {
return new RpcResult();
}
}
}
package com.fox.config;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* dubbo白名单
*
* @author
* @create 2018-04-11 10:38
**/
@Component
public class IpWhiteList {
//白名单是否禁用
public Boolean isEnabled() {
return true;
}
//白名单列表
List<String> getAllowedIps() {
List<String> whiteList = new ArrayList<>();
whiteList.add("192.168.30.9");
whiteList.add("192.168.20.1");
return whiteList;
}
}
在resources下添加com.fox.config.AuthorityFilter纯文本文件目录如上图,内容为
authorityFilter=com.fox.config.AuthorityFilter
最后在提供服务的service上添加filter
package com.fox.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.fox.api.ProviderApiService;
import com.fox.service.ProviderService;
import javax.annotation.Resource;
/**
* Created by sh00859 on 2018/4/10.
*/
@Service(filter = "authorityFilter")
public class ProviderApiServiceImpl implements ProviderApiService {
@Resource
private ProviderService providerService;
@Override
public String queryDubboTestName(String name) {
return providerService.queryDubboTestName(name);
}
}
上述几个步骤实现了Filter的基础功能,在whiteList当中的ip可以访问,在访问时会根据消费者的ip自动找到相应可以访问的提供者服务
二、dubbo各种参数配置
当修改接口或者重构接口又不影响老接口使用时可以利用version和group来实现
@Reference(group = "group1")
private ProviderApiService providerApiService;
@Reference(group = "group2")
private ProviderApiService providerApiService2;
当本地测试需要连接指定主机时可配置url来实现直接RPC
@Reference(url = "dubbo://192.168.20.8:20880")
private ProviderApiService providerApiService;
对于热点数据可以开启dubbo的cache来缓存数据,对于幂等的请求会优先查询缓存结果
@Reference(cache = "true")
private ProviderApiService providerApiService;
三、dubbo调用流程
提供者启动服务时会将服务注册到注册中心,消费者启动服务时会从注册中心拉取提供者列表及相关策略(负载均衡)缓存到本地,调用时直接通过本地缓存的连接地址访问远程服务,当注册中心宕机后不影响正常调用,当提供者提供的服务或者策略发生变化并通知到注册中心时,注册中心会通知消费者重新缓存提供者的列表或者策略到本地
四、dubbo的负载均衡策略
随机、轮询、最小请求数、一致hash
RandomLoadBalance:
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // 总个数
int totalWeight = 0; // 总权重
boolean sameWeight = true; // 权重是否都一样
for (int i = 0; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
totalWeight += weight; // 累计总权重
if (sameWeight && i > 0
&& weight != getWeight(invokers.get(i - 1), invocation)) {
sameWeight = false; // 计算所有权重是否一样
}
}
if (totalWeight > 0 && ! sameWeight) {
// 如果权重不相同且权重大于0则按总权重数随机
int offset = random.nextInt(totalWeight);
// 并确定随机值落在哪个片断上
for (int i = 0; i < length; i++) {
offset -= getWeight(invokers.get(i), invocation);
if (offset < 0) {
return invokers.get(i);
}
}
}
// 如果权重相同或权重为0则均等随机
return invokers.get(random.nextInt(length));
}