一 负载均衡原理
负载均衡为将应用的并发处理能力分摊到不同的机器中 机器上水平分布着相同功能的应用,实现负载均衡需要考虑到服务器的容灾能力,服务器的动态配置等
实现场景:
假设在服务端存在一个webservice 部署在58.131,58.132上 客户端每次访问压力最小的服务器
从零开发 需要当服务器58.131和58.132启动时 要在zookooper集群中注册临时节点 客户端订阅zookeeper该临时节点 客户端使用负载均衡算法 当访问服务器开始时 修改zookeeper上该服务器节点的值累加1 完成访问后 减1实现相对复杂
Alibaba的dubbo框架实现了在zokeeper自动注册服务器 客户端自动订阅和缓存数据 以及负载均衡算法的功能 这里使用maven来添加dubbo和webservice依赖 dubbo必须依靠spring来进行管理
服务端代码
webservice接口
package cn.et.dubbo;
public interface DemoService {
public String sayHello(String name);
}
实现类
package cn.et.dubbo;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 提供方应用信息,用于计算依赖关系 name就是项目名称 -->
<dubbo:application name="uuid" />
<!--注册服务器 当该服务启动时 自动将服务器信息写入 zookeeper服务器中
如果是zookeeper集群 address用逗号隔开
-->
<dubbo:registry address="192.168.58.1:2181" protocol="zookeeper" session="5000" timeout="1">
</dubbo:registry>
<!-- port和tomcat的端口必须一致 web.xml配置的servlet拦截的/*
访问都中http://当前ip:8080/上下文路径/cn.et.dubbo.DemoService?wsdl
-->
<dubbo:protocol name="webservice" port="8080" server="servlet" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.et.dubbo.DemoService" protocol="webservice"
ref="demoService" />
<bean id="demoService" class="cn.et.dubbo.DemoServiceImpl"></bean>
</beans>
Web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring.xml</param-value>
</context-param>
<!--spring的配置-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dubbo</servlet-name>
<servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dubbo</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
maven添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.7</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.0.M2</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
以上程序部署到 58.131,58.132的tomcat中 运行zkcli查看 /duddo节点下的子节点信息
有时 在linux启动时会抛出异常 bogon: bogon: Name or service not known, dubbo version: 2.4.7, current host:
因为dubbo尝试获取linux主机名后获取对应的主机ip
使用命令 hostname查看 主机名 为bogon
修改 /etc/hosts添加一行
bogon 当前主机的ip
比如 58.131机器上添加 bogon 192.168.58.131
启动后 访问
http://192.168.58.131:8080/uuid/cn.et.dubbo.DemoService?wsdl
http://192.168.58.132:8080/uuid/cn.et.dubbo.DemoService?wsdl
使用zkcli客户端查看zookeeper注册中心注册信息
[zk: 192.168.58.1:2181(CONNECTED) 258] ls /dubbo/cn.et.dubbo.DemoService/providers
[webservice%3A%2F%2F192.168.58.132%3A8080%2Fcn.et.dubbo.DemoService%3Fanyhost%3D
true%26application%3Duuid%26dubbo%3D2.4.7%26interface%3Dcn.et.dubbo.DemoService%
26methods%3DsayHello%26pid%3D4664%26server%3Dservlet%26side%3Dprovider%26timesta
mp%3D1495539889340, webservice%3A%2F%2F192.168.58.131%3A8080%2Fcn.et.dubbo.DemoS
ervice%3Fanyhost%3Dtrue%26application%3Duuid%26dubbo%3D2.4.7%26interface%3Dcn.et
.dubbo.DemoService%26methods%3DsayHello%26pid%3D4494%26server%3Dservlet%26side%3
Dprovider%26timestamp%3D1495539894062]
[zk: localhost:2181(CONNECTED) 259]
【注意点1】
<dubbo:registry address="192.168.58.1:2181" protocol="zookeeper" session="5000" timeout="1">
这里设置的session超时时间和连接timeout 都是无效的 需要自己重写部分api
在dubbojar包 dubbo/internal/com.alibaba.dubbo.remoting.zookeeper.ZookeeperTrasporter资源文件 里面定义了 使用不同客户端调用zookeer的代码
里面有使用zkclient和curator的代码
zkclient=com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperTransporter
我们在自己的项目中新建一个相同的类 代码 拷贝相同
package cn.et.dubbo;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.zookeeper.ZookeeperClient;
import com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter;
public class ZkclientZookeeperTransporter implements ZookeeperTransporter {
public ZookeeperClient connect(URL url) {
return new ZkclientZookeeperClient(url);
}
}
重写 ZkclientZookeeperClient类
package cn.et.dubbo;
import java.util.List;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.I0Itec.zkclient.exception.ZkNodeExistsException;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.zookeeper.ChildListener;
import com.alibaba.dubbo.remoting.zookeeper.StateListener;
import com.alibaba.dubbo.remoting.zookeeper.support.AbstractZookeeperClient;
public class ZkclientZookeeperClient extends AbstractZookeeperClient<IZkChildListener> {
private final ZkClient client;
private volatile KeeperState state = KeeperState.SyncConnected;
public ZkclientZookeeperClient(URL url) {
super(url);
//默认的代码是client = new ZkClient(url.getBackupAddress())
client = new ZkClient(url.getBackupAddress(),5000,1000);//这里自己设置这个超时和连接时间
client.subscribeStateChanges(new IZkStateListener() {
public void handleStateChanged(KeeperState state) throws Exception {
ZkclientZookeeperClient.this.state = state;
if (state == KeeperState.Disconnected) {
stateChanged(StateListener.DISCONNECTED);
} else if (state == KeeperState.SyncConnected) {
stateChanged(StateListener.CONNECTED);
}
}
public void handleNewSession() throws Exception {
stateChanged(StateListener.RECONNECTED);
}
});
}
public void createPersistent(String path) {
try {
client.createPersistent(path, true);
} catch (ZkNodeExistsException e) {
}
}
public void createEphemeral(String path) {
try {
/*
这里 %2F为 / 重写添加上下文路径
也就是/cn.et.dubbo.DemoService 替换为 /uuid/cn.et.dubbo.DemoService
订阅者 从 zookeeper中获取可以访问的服务器webservice地址 注册时不会添加上下文路径 所以我们
需要添加上下文路径 才能正常调用
如果在提供者 spring.xml中添加
<dubbo:protocol name="webservice" port="8080" server="servlet" path="uuid"/>
dubbo在上下文路径后在加上一层 uuid
访问编程了 http://localhost:8080/uuid/uuid/cn.et.dubbo.DemoService?wsdl
* */
if(path.indexOf("%2Fcn.et.dubbo.DemoService")>0){
path=path.replaceAll("%2Fcn.et.dubbo.DemoService", "%2Fuuid%2Fcn.et.dubbo.DemoService");
}
client.createEphemeral(path);
} catch (ZkNodeExistsException e) {
}
}
public void delete(String path) {
try {
client.delete(path);
} catch (ZkNoNodeException e) {
}
}
public List<String> getChildren(String path) {
try {
return client.getChildren(path);
} catch (ZkNoNodeException e) {
return null;
}
}
public boolean isConnected() {
return state == KeeperState.SyncConnected;
}
public void doClose() {
client.close();
}
public IZkChildListener createTargetChildListener(String path, final ChildListener listener) {
return new IZkChildListener() {
public void handleChildChange(String parentPath, List<String> currentChilds)
throws Exception {
listener.childChanged(parentPath, currentChilds);
}
};
}
public List<String> addTargetChildListener(String path, final IZkChildListener listener) {
return client.subscribeChildChanges(path, listener);
}
public void removeTargetChildListener(String path, IZkChildListener listener) {
client.unsubscribeChildChanges(path, listener);
}
}
修改完成将编译好的类 添加到jar包中 修改jar包中dubbo/internal/com.alibaba.dubbo.remoting.zookeeper.ZookeeperTrasporter文件zkclient=cn.et.dubbo。ZkclientZookeeperTransporter
【注意点2】实际访问地址为192.168.58.132:8080//uuid/cn.et.dubbo.DemoService?wsdl 前面带了上下文路径
zookeeper中注册的确实192.168.58.132:8080/cn.et.dubbo.DemoService 订阅者(客户端) 获取这个地址肯定无法调用 (上面ZkclientZookeeperClient类
createEphemeral标红的部分就解决了 上下文路径注册的问题
)
客户端调用的配置
spring.xml
<?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-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="uuid" />
<dubbo:registry address="192.168.58.1:2181" protocol="zookeeper" session="5000" timeout="1" >
</dubbo:registry>
<!-- 声明需要暴露的服务接口 -->
<dubbo:reference interface="cn.et.dubbo.DemoService" id="mydemo" >
</dubbo:reference>
</beans>
pom.xml 依赖和服务器端一致
java调用代码
package cn.et.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
/**
*时间:2017-5-25 下午08:20:17
*作者: LM
*联系方式:973465719@qq.com
* @param args
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"/spring.xml"});
DemoService demo=(DemoService)context.getBean("mydemo");
System.out.println(demo.sayHello("jiaozi"));
System.in.read(); // 按任意键退出
}
}
此时交换停止 58.131和 58.132 运行客户端main方法 测试webservice是否能够调用 我这里正常调用