dubbo可否支持多个zookeeper
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo可否支持多个zookeeper相关的知识,希望对你有一定的参考价值。
参考技术A 可以,但是配置文件里配置的地址必须的zookeeper集群的地址。 参考技术B dubbo、zookeeper、maven的相关技术请参考官方文档或者网上博客文章,这边不一一赘述(注意:本篇描述的都是基于windows系统,Linux系统下的部署请参考其他文章)。http://www.java-bbs.com/thread-402-1-1.html
dubbo+zookeeper的简易实现
分布式项目首先想到的问题是如何再多个服务之间进行数据传递和接口调用
1、创建两个服务,一个controller,一个service
service层的实现类使用的service注解,是alibaba的,不是jdk的
//service层的一个类
package com.ghh.service; import com.alibaba.dubbo.config.annotation.Service; @Service public class TestServiceImpl implements TestService { @Override public String getName() { return "张三"; } }
//接口
public interface TestService {
public String getName();
}
//service服务的applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="dubbodemo-service"/> <!--配置连接的端口号--> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> //虚拟机上的zookeeper的地址 <!-- 扫描带有service的注解,注册到zeekooper的注册中心--> <dubbo:annotation package="com.ghh" /> </beans>
//service服务的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2、controller服务的
@Reference //使用的是alibaba的注解,不是jdk的
@RestController @RequestMapping("/test") public class TestController { @Reference private TestService testService; @RequestMapping("/getName") public String getName(){ System.out.println("123"); String name = testService.getName(); return name; } @RequestMapping("/me") public String name(){ return "张三"; } }
此处,我没有导入service服务的jar包,我直接将service服务的接口导到controller服务中,直接调用
controller服务的applicationContext-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven > <!-- 将传输的字符串强制转换成utf-8编码, 防止中文乱码 --> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 给当前项目服务起个名--> <dubbo:application name="dubboxdemo-web" /> <!-- 配置连接zookepper --> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> <!-- 配置包扫描, 只有在这个包下面才可以注入service --> <dubbo:annotation package="com.ghh" /> </beans>
//controller服务的web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- 解决post乱码 --> <filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springMvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、总结
dubbo+zookeeper的实现,是将各服务注册到zookeeper,controller通过@Reference注解,从zookeeper中拿到service的代理类对象,service中通过alibaba的@service注解注册到zookeeper中
实现了两个服务之间的跨服调用,接口调用。
以上是关于dubbo可否支持多个zookeeper的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo配合SpringBoot,实现接口多个实现(group)