虽然项目中用到了dubbo,还是想自己玩玩写个demo。百度一搜,关于dubbo的helloworld基本都是在windows上做的。现在在Linux上玩一下。
环境准备:
jdk版本:1.7
服务器:tomcat7
dubbo版本:2.5.4
注册中心zookeeper版本:zookeeper-3.4.5.tar
dubbo-admin版本:dubbo-admin-2.5.4.war
系统:CentOS 6.4
准备三台虚拟机 我用的centos6.4,具体安装配置可以看这里https://www.cnblogs.com/lihaoyang/p/8341760.html,做zookeeper集群,部署管控台
说明:
1,JDK版本选择:从17年开始dubbo又开始更新后的第一个版本是2.5.4,之前版本一直停留在2.5.3,所以现在线上跑的项目很多还是2.5.3版本的dubbo。现在很多新项目都用的jdk1.8,但是用dubbo2.5.4版本的管控台,在jdk1.8环境上跑启动tomcat会报错(一个URIType类里的getset方法导致的,原因是这个set方法写的不规范,虽然网上也有一些解决办法,但是还是换用JDK1.7吧!毕竟线上现在JDK1.7还是很多的),所以我这里还用JDK1.7;
2,dubbo版本:为了和dubbo版本保持一致,所以管控台也用2.5.4 。最新版本的dubbo源码项目结构已经跟以前不一样了(详看github),2.5.4版本的管控台dubbo-admin项目也不存在了,所以要从dubbo-admin的源码构建出war包也构建不了了(我上传到了百度网盘一份dubbo-admin-2.5.4.war:https://pan.baidu.com/s/1iUzL2YuDU0I2MBQU5jnG4w 提取码:b4t1)。
一:安装Zookeeper集群
http://www.cnblogs.com/lihaoyang/p/8358153.html
安装好zookeeper后, zkServer.sh start 启动每台zk,zkServer.sh status 查看每个节点状态,可以看到一个leader,两个follower
二:安装管控台
把 dubbo-admin-2.5.4.war 上传到其中的一台机器,copy到tomcat7的webapps下,启动tomcat解压war包,或者 unzip dubbo-admin-2.5.4.war -d dubbo-admin 命令解压,删除tomcat的webapps下的ROOT,并把解压后的管控台命名为ROOT这样访问的时候就不用输入项目名了。
进入 /lhy/software/apache-tomcat-7.0.70/webapps/ROOT/WEB-INF 修改dubbo管控台配置
vim dubbo.properties:用户名密码都改为了root,修改zk集群地址
保存并退出wq! 。
启动tomcat,关闭每台机器的防火墙,在windows机器上访问tomcat机器地址,输入配置的root/root用户
可以看到dubbo管控台安装没问题:
由于用的是dubbo-2.5.4版本,所以这一步特别注意JDK的版本一定要是JDK1.7 ,如果你是jdk1.8 启动管控台会报错。
三:写java程序
项目结构:比较接近实际项目结构了
lhy-parent父工程,定义jar包版本。
lhy-facede:定义dubbo接口,为了省事把 实体类放在了lhy-facade,这样生产者和消费者都能引用了。现公司的接口层叫xxx-api,实现叫xxx-service,而里边的接口和实现类的定义叫UserFacade、UserFacadeImpl。其实接口定义为XXXFacade比较科学,这样在消费者项目里,一看就知道是dubbo接口,而区别于本项目的service。
lhy-service:接口实现
lhy-consumer:消费者
lhy-parent 父项目pom工程:
其中zookeeper注册中心依赖的jar包是zkClient,官方文档说了。从2.2.0版本zk注册中心默认使用zkclient,需依赖
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
或者从2.3.0版本开始,支持zk注册中心支持curator实现,需依赖
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator>framework</artifactId>
<version>1.1.10</version>
</dependency>
本例子使用zkClient
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.lhy</groupId> <artifactId>lhy-parent</artifactId> <version>1.0</version> <packaging>pom</packaging> <modules> <module>../lhy-facade</module> <module>../lhy-service</module> <module>../lhy-consumer</module> </modules> <!-- 集中定义依赖版本号 --> <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> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.4.2-fix</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> <shiro.version>1.3.2</shiro.version> <commons-logging.version>1.2</commons-logging.version> <log4j.version>1.2.17</log4j.version> <slf4j-log4j12.version>1.7.5</slf4j-log4j12.version> <zkclient.version>0.1</zkclient.version> <dubbo.version>2.5.4</dubbo.version> <mybatis-plus.version>3.0.5</mybatis-plus.version> </properties> <!-- 只定义依赖的版本,并不实际依赖 --> <dependencyManagement> <dependencies> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--mysql end--> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!--连接池end--> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!--spring end--> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!--Jsp end--> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- 工具类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!--<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>${commons-logging.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j-log4j12.version}</version> </dependency> <!– Jackson Json处理工具包 –> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!–模板语言支持包–> <!– velocity模板语言支持包 –> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!– velocity-tools –> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency> <!– 不加这个配置volecity视图解析器报错 –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.0.RELEASE</version> </dependency>--> <!-- 当当elastic job --> <!--<dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-core</artifactId> <version>2.1.5</version> </dependency> <!– 使用springframework自定义命名空间时引入 –> <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring</artifactId> <version>2.1.5</version> </dependency>--> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
接口层 lhy-facade:
是一个jar包工程,只提供接口,供现实层lhy-service 和 消费者 lhy-consumer 依赖,为了简便,把pojo也放这里了,这样实现和消费者项目两者都能依赖。
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> <packaging>jar</packaging> <artifactId>lhy-facade</artifactId> <parent> <groupId>com.lhy</groupId> <version>1.0</version> <artifactId>lhy-parent</artifactId> <relativePath>../lhy-parent/pom.xml</relativePath> </parent> </project>
User.java:
package com.lhy.service.pojo; import java.io.Serializable; //实现序列化接口 public class User implements Serializable{ private int id; private String name; private int sex; //省略getset }
接口 UserFacade.java
package com.lhy.service.user; import com.lhy.service.pojo.User; import java.util.List; public interface UserFacade { public User getById(int id); public int addUser(User user); public List<User> getUsers(); }
接口实现层 lhy-service:
war包工程,依赖父工程和接口层。dao层为了没有单独出来,这里依赖了一个mybatis-plus的mybatis 插件,为了简便没有查库,但是dao的配置没有少,为了完整性、
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.lhy</groupId> <artifactId>lhy-service</artifactId> <version>1.0</version> <packaging>war</packaging> <name>lhy-service</name> <parent> <groupId>com.lhy</groupId> <version>1.0</version> <artifactId>lhy-parent</artifactId> <relativePath>../lhy-parent/pom.xml</relativePath> </parent> <dependencies> <!--接口层--> <dependency> <groupId>com.lhy</groupId> <version>1.0</version> <artifactId>lhy-facade</artifactId> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <scope>provided</scope> </dependency> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <!--数据源相关--> <!--连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mysql end--> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <!--zkClient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resource</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> </plugin> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 这里配置端口号和访问路径 --> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
web.xml :
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <display-name>lhy-service</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 解决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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>lhy-consumer</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>lhy-consumer</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- <error-page> <error-code>404</error-code> <location>/static/errorPage/404.html</location> </error-page> <error-page> <error-code>403</error-code> <location>/static/errorPage/403.html</location> </error-page>--> </web-app>
Spring配置:
spring-context.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 扫描包加载Service实现类 --> <context:component-scan base-package="com.lhy.service.impl"></context:component-scan> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 配置扫描包,加载mapper代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lhy.service.mapper"></property> </bean> <!--调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory--> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 定义事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!--dubbo配置--> <import resource="dubbo-provider.xml"/> </beans>
springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <context:component-scan base-package="com.lhy.service.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/page/" /> <property name="suffix" value=".jsp" /> </bean> <!--配置velocity 前端的模板语言--> <!--velocity配置--> <!-- <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/template/"/> <property name="velocityProperties"> <props> <prop key="input.encoding">utf-8</prop> <prop key="output.encoding">utf-8</prop> <prop key="file.resource.loader.cache">false</prop> <prop key="file.resource.loader.modificationCheckInterval">1</prop> <prop key="velocimacro.library.autoreload">false</prop> </props> </property> </bean> --> <!--velocity模板语言--> <!-- <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="suffix" value=".vm"/> <property name="contentType" value="text/html;charset=utf-8"/> <property name="dateToolAttribute" value="date"/>日期函数名称 </bean> --> <!-- 静态资源映射 --> <mvc:resources mapping="/static/**" location="/static/"/> <mvc:resources mapping="/css/**" location="/css/"/> <!-- 定义文件上传解析器 --> <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 设定默认编码 <property name="defaultEncoding" value="UTF-8"></property> 设定文件上传的最大值5MB,5*1024*1024 <property name="maxUploadSize" value="5242880"></property> </bean> --> <!-- 开启aop代理 ,spring-shirl里就可以不用配置lifecycleBeanPostProcessor了--> <aop:config proxy-target-class="true"></aop:config> <!-- 全局异常处理 ,只要实现了HandlerExceptionResolver接口的就是全局异常处理器--> <!-- <bean class="com.lhy.shiro.exception.CustomExceptionResolver"/> --> </beans>
dubbo-provider.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--提供方应用信息,用户计算依赖关系--> <dubbo:application name="lhy-service"/> <!--使用zookeeper注册中心暴露服务地址--> <dubbo:registry address="zookeeper://192.168.85.3:2181?backup=192.168.85.4:2181,192.168.85.5:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 ,写操作可设置retries="0"避免重复调用SOA服务--> <dubbo:service retries="0" interface="com.lhy.service.user.UserFacade" ref="userFacade" /> </beans>
db.properties :
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/lhy?characterEncoding=utf-8 jdbc.username=root jdbc.password=root
接口实现类:UserFacade.java
package com.lhy.service.impl; import com.lhy.service.pojo.User; import com.lhy.service.user.UserFacade; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service("userFacade") public class UserFacadeImpl implements UserFacade{ @Override public User getById(int id) { User user = new User(); user.setId(id); user.setName("张三"); return user; } @Override public int addUser(User user) { return 1; } @Override public List<User> getUsers() { List<User> list = new ArrayList<>(); User u1 = new User(); u1.setId(1); u1.setName("阿皮"); User u2 = new User(); u2.setId(2); u2.setName("喜觉"); User u3 = new User(); u3.setId(3); u3.setName("虚竹"); User u4 = new User(); u3.setId(4); u3.setName("wooubeibei"); list.add(u1); list.add(u2); list.add(u3); list.add(u4); return list; } }
消费者 lhy-consumer :
war包工程,依赖父工程和接口层,配置和接口实现差不多,
不一样的是dubbo配置:
dubbo-consumer.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="lhy-consumer" /> <!--使用zookeeper注册中心暴露服务地址--> <dubbo:registry address="zookeeper://192.168.85.3:2181?backup=192.168.85.4:2181,192.168.85.5:2181"/> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="userFacade" interface="com.lhy.service.user.UserFacade" /> </beans>
定义一个UserService,实现类里调用 dubbo接口:
package com.lhy.consumer.service.impl; import com.lhy.consumer.service.UserService; import com.lhy.service.pojo.User; import com.lhy.service.user.UserFacade; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserFacade userFacade; @Override public List<User> getUsers() { return userFacade.getUsers(); } }
UserController来调用一下:
package com.lhy.consumer.controller; import com.lhy.consumer.service.UserService; import com.lhy.service.pojo.User; import com.lhy.service.user.UserFacade; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/hello") @ResponseBody public String hello(){ return "hello"; } @RequestMapping("/toHelloPage") public String toHelloPage(){ return "hello"; } @RequestMapping("/getUsers") public @ResponseBody List<User> getUsers(){ List<User> list = userService.getUsers(); return list; } }
代码打好后,先把接口层mvn install到本地仓库,依次启动 生产者 lhy-service 、消费者lhy-consumer :
访问一下消费者:http://localhost:8081/user/getUsers
可以看到,成功调用了提供者的接口,再看dubbo管控台:有了一个服务
可以利用管控台对服务进行各种治理了
只用zk查看工具看zk上注册的信息: