zookeeper+dubbo+nginx集群和负载均衡简单例子

Posted yulong123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zookeeper+dubbo+nginx集群和负载均衡简单例子相关的知识,希望对你有一定的参考价值。

1:服务层面用Dubbo+Zookeeper实现分布式服务,然后Http web层用 Nginx 实现高可用集群方案,本文记录下demo例子

2:windows环境,  zk 3台伪集群 ,idea,maven

3:zk集群已搭建好,首先开起zk集群  cd /bin 目录下 zkcli.cmd -server 127.0.0.1:2181 开启zk客户端 ls / 能正常连接到server 表示集群开启成功

4:idea新建maven project  dubbo-server

5:dubbo-server下新建2个module模块 server-api,server-provider

   server-api 接口类 

    

package com.cn.dubbo;

public interface IGpHello {

String sayHello(String msg);

}

 

   server-provider 接口实现类

   server-provider下的pom.xml引入dubbo,zookeeper相关jar

  

<?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">
<parent>
<artifactId>dubbo-server</artifactId>
<groupId>com.cn.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>server-provider</artifactId>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cn.dubbo</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>
</dependencies>
</project>

api中的实现类1
package com.cn.dubbo;

import com.alibaba.dubbo.rpc.RpcContext;

public class GpHelloImpl implements IGpHello {

public String sayHello(String msg) {
return "Hello " + msg + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
api中的实现类2 
package com.cn.dubbo;

import com.alibaba.dubbo.rpc.RpcContext;

public class GpHelloImpl2 implements IGpHello {

public String sayHello(String msg) {
return "Hello " + msg + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}
}
resources目录下配置2dubbo xml文件分别对应上面2个实现类
dubbo-client1.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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--提供方信息-->
<dubbo:application name="dubbo-server" owner="yulong" />
<!--注册中心-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>
<!--多协议配置-->
<dubbo:protocol name="dubbo" port="28081"/>
<dubbo:service interface="com.cn.dubbo.IGpHello" ref="helloService"
protocol="dubbo" />
<bean id="helloService" class="com.cn.dubbo.GpHelloImpl" />
dubbo-client2.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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!--提供方信息-->
<dubbo:application name="dubbo-server" owner="yulong" />
<!--注册中心-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>
<!--多协议配置-->
<dubbo:protocol name="dubbo" port="28082"/>
<dubbo:service interface="com.cn.dubbo.IGpHello" ref="helloService"
protocol="dubbo" />
<bean id="helloService" class="com.cn.dubbo.GpHelloImpl2" />
</beans></beans>
新建2个main方法移动对应的服务
BootstrapCluster1
package com.cn.dubbo;

import com.alibaba.dubbo.container.Main;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class BootstrapCluster1 {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("META-INF/spring/dubbo-cluster1.xml");
context.start();
//dubbo自带了springcontainer log4jcontainer等容器 可以直接main方式启动容器
//Main.main(new String[]{"spring"});
System.out.println("服务启动成功");
System.in.read();//阻塞进程
}
}
BootstrapCluster2
package com.cn.dubbo;

import com.alibaba.dubbo.container.Main;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class BootstrapCluster2 {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("META-INF/spring/dubbo-cluster2.xml");
context.start();
//dubbo自带了springcontainer log4jcontainer等容器 可以直接main方式启动容器
//Main.main(new String[]{"spring"});
System.out.println("服务启动成功");
System.in.read();//阻塞进程
}
}
同时启动可以通过配置dubbo-admin管理页面查看服务是否启动,我这直接 ls /dubbo/com.cn.dubbo.IGpHello/providers下已经成功注册了2个服务

[dubbo%3A%2F%2F172.20.10.6%3A28081%2Fcom.cn.dubbo.IGpHello%3Fanyhost%3Dtrue%26ap
plication%3Ddubbo-server%26dubbo%3D2.5.3%26interface%3Dcom.cn.dubbo.IGpHello%26m
ethods%3DsayHello%26owner%3Dyulong%26pid%3D4864%26side%3Dprovider%26timestamp%3D
1543887881068, dubbo%3A%2F%2F172.20.10.6%3A28082%2Fcom.cn.dubbo.IGpHello%3Fanyho
st%3Dtrue%26application%3Ddubbo-server%26dubbo%3D2.5.3%26interface%3Dcom.cn.dubb
o.IGpHello%26methods%3DsayHello%26owner%3Dyulong%26pid%3D5572%26side%3Dprovider%
26timestamp%3D1543887965460]

6:搭建2个简单的springmvc web

    springmvc工程

    6.1 :配置pom.xml 引入spring dubbo zk相关jar依赖

   <?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.cn.mvc</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>springmvc Maven Webapp</name>
<!-- FIXME change it to the project‘s website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--spring版本-->
<spring.version>4.0.5.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.cn.dubbo</groupId>
<artifactId>server-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>

</dependencies>

<build>
<finalName>springmvc</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

  6.2:web.xml中配置

 <!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>
<display-name>Archetype Created Web Application</display-name>
<!--Spring MVC 配置 并添加监听-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-client.xml</param-value>
</context-param>

<!-- 字符过滤器 传值乱码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--配置前端控制器 进行请求分发 DispatcherServlet本质也是一个Servlet -->
<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:mvc-dispatcher.xml</param-value>
</init-param>
<!--标记容器启动的时候就启动这个servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--拦截所有-->
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

index.jsp
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<body>
<a href="/developer/hello">dubbo 测试1</a>
</body>
</html>
6.3:resources目录下配置mvc-dispathcer.xml和dubbo-client.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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置包扫描器 -->
<context:component-scan base-package="com.cn.mvc.controller"/>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<?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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供方信息-->
<dubbo:application name="dubbo-client" owner="yulong" />
<!--注册中心-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>
<dubbo:reference id="helloService" interface="com.cn.dubbo.IGpHello" protocol="dubbo" />
</beans>
6.4:新建MyController类
package com.cn.mvc.controller;

import com.cn.dubbo.IGpHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Scope("prototype")
@Controller
@RequestMapping("/developer")
public class MyController {

@Autowired
private IGpHello helloService;

@ResponseBody
@RequestMapping(value = "/hello", produces = "application/json; charset=utf-8")
public String hello() throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = helloService.sayHello("我是消费者1号!"+ sf.format(new Date()));
//String string = new String(str.getBytes("ISO-8859-1"), "utf-8");
System.out.println(str);
return str;
}
}
7:新建工程springmvc2
springmvc2和1差不多
index.jsp 和mycontroler类有点差别,其他配置一样
<html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<body>
<a href="/developer/hello">dubbo 测试2</a>
</body>
</html>

package com.cn.mvc.controller;

import com.cn.dubbo.IGpHello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Scope("prototype")
@Controller
@RequestMapping("/developer")
public class MyController {

@Autowired
private IGpHello helloService;

@ResponseBody
@RequestMapping(value = "/hello", produces = "application/json; charset=utf-8")
public String hello() throws UnsupportedEncodingException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = helloService.sayHello("我是消费者2号!"+sf.format(new Date()));
//String string = new String(str.getBytes("ISO-8859-1"), "utf-8");
System.out.println(str);
return str;
}
}
分别启动2个工程注意tomca端口不能占用 我用的是8081 8082
点击测试1发现能正常返回

Hello 我是消费者1号!2018-12-04 10:00:50, response form provider: 172.20.10.6:28081
点击测试2发现能正常返回 
Hello 我是消费者2号!2018-12-04 10:02:10, response form provider: 172.20.10.6:28082

cmd 客户端下发现有2个消费者

[zk: 127.0.0.1:2181(CONNECTED) 9] ls /dubbo/com.cn.dubbo.IGpHello/consumers
[consumer%3A%2F%2F172.20.10.6%2Fcom.cn.dubbo.IGpHello%3Fapplication%3Ddubbo-clie
nt%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.5.6%26interface%3Dcom.cn.d
ubbo.IGpHello%26methods%3DsayHello%26owner%3Dyulong%26pid%3D644%26protocol%3Ddub
bo%26revision%3D1.0-SNAPSHOT%26side%3Dconsumer%26timestamp%3D1543888927684, cons
umer%3A%2F%2F172.20.10.6%2Fcom.cn.dubbo.IGpHello%3Fapplication%3Ddubbo-client%26
category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.5.6%26interface%3Dcom.cn.dubbo.
IGpHello%26methods%3DsayHello%26owner%3Dyulong%26pid%3D4652%26protocol%3Ddubbo%2
6revision%3D1.0-SNAPSHOT%26side%3Dconsumer%26timestamp%3D1543888846966]
[zk: 127.0.0.1:2181(CONNECTED) 10]

8:dubbo默认是随机策略访问zk服务节点,最后把Nginx也配置上去下载后直接解压 进入到配置文件中心 修改Nginx.conf  

# 配置负载均衡
upstream dubbo-test-server {
# 服务器地址
server 127.0.0.1:8081 weight=10;
server 127.0.0.1:8082 weight=10;
}

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#root html;
#index index.html index.htm;
proxy_pass http://dubbo-test-server;
}

修改后保存启动nginx

浏览器输入 http://localhost/developer/hello 发现能正常访问服务

以上是简单的demo,记录下自己的实践

 

 

 

 

 

 

 

 





 







































































































































































































































































































































































































































































以上是关于zookeeper+dubbo+nginx集群和负载均衡简单例子的主要内容,如果未能解决你的问题,请参考以下文章

Java集群优化——dubbo+zookeeper构建高可用分布式集群

[置顶] Java集群优化——dubbo+zookeeper构建高可用分布式集群

[置顶] Java集群优化——dubbo+zookeeper构建高可用分布式集群

dubbo和zookeeper

zookeeper的负载均衡能取代nginx吗

zookeeper的负载均衡能取代nginx吗