服务化治理(SOA)及DUBBO(二)实践干货
jdk7、tomcat、eclipse、maven
- 下载地址: http://www.apache.org/dist/zookeeper/
- 解压到安装目录,并装zoo_sample.cfg文件更名为zoo.cfg
编辑zoo.cfg中dataDir配置,修改数据存放位置
如dataDir=E:\\workspace_dubbo\\zookeeper-3.4.8\\data - 双击zkServer.cmd启动注册中心服务(Linux 运行 zkServer.sh start )
- dubbo-admin-2.5.3.war
下载地址:http://dubbo.io/Download-zh.htm - 解压到tomcat的webapps/ROOT中
修改webapps/ROOT/WEB-INF中的 dubbo.properties文件,配置注册中心地址,我们使用上面安装的Zookeeper 的注册中心如:dubbo.registry.address=zookeeper://127.0.0.1:2181 - 访问http://localhost:8080/ROOT测试安装是否成功
- 代码组织
- 提供者开发
- 消费者开发
如果出现登陆窗口,说明dubbo控制台已安装成功,用root/root进入
以微服务架构来组织代码,一个模块功能一个项目,一个项目又按上面的结构分层组织。
如用户基础服务和订单基础服务两个模块
用户服务
bs-account-api
bs-account-entity
bs-account-internalapi
bs-account-service
bs-account-openapi
bs-account-sdk
bs-tools
订单服务
bs-order-api
bs-order-entity
bs-order-internalapi
bs-order-service
基础服务下的各个服务模块可以共享bs-tools
新建Maven工程dubbo_demo_root,作为其它模块的父项目,方便后面做一些统一配置
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo_demo_root</name> <description>dubbo_demo_root</description> <packaging>pom</packaging> <modules> <module>dubbo_demo_api</module> <module>dubbo_demo_entity</module> <module>dubbo_demo_internalapi</module> <module>dubbo_demo_service</module> <module>dubbo_demo_client</module> </modules> </project> |
新建Maven工程dubbo_demo_entity
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 | <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> <parent> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo_demo_entity</artifactId> <name>dubbo_demo_entity</name> </project> |
新建实体类User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package cn.jslfl.demo.dubbo.entity; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 5144095165528387447L; private String userName; private int age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "{" + "userName:" + userName + "," + "age:" + age + "," +"}"; } } |
实体必须要实现序列化接口
新建Maven工程dubbo_demo_internalapi
pom中加入依赖dubbo_demo_entity
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <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> <parent> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo_demo_internalapi</artifactId> <name>dubbo_demo_internalapi</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project> |
新建接口IUserService
1 2 3 4 5 6 7 8 9 | package cn.jslfl.demo.dubbo.interalapi; import cn.jslfl.demo.dubbo.entity.User; public interface IUserService { public String hello(String name); public String getUserInfo(User user); } |
新建Maven工程dubbo_demo_service
pom中加入依赖dubbo_demo_internalapi以及junit、dubbo、zookeeper、zkclient
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <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> <parent> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo_demo_service</artifactId> <name>dubbo_demo_service</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 依赖接口定义 --> <dependency> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_internalapi</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 依赖dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!--zookeeper服务--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <!--zookeeper客户端--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </project> |
开发接口实现类UserServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package cn.jslfl.demo.dubbo.service; import cn.jslfl.demo.dubbo.entity.User; import cn.jslfl.demo.dubbo.interalapi.IUserService; public class UserServiceImpl implements IUserService { public String hello(String name){ return "Hi! " + name + ",你好."; } public String getUserInfo(User user) { return user.toString(); } } |
新建Maven工程dubbo_demo_api
pom中加入依赖dubbo_demo_service以及spring-web、javax.servlet-api
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <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> <parent> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo_demo_api</artifactId> <name>dubbo_demo_api WEB App</name> <packaging>war</packaging> <dependencies> <dependency> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project> |
resources下新建dubbo provider配置文本spring-dubbo-provider.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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-provider-demo" /> <!-- 注册中心服务地址,使用zookeepert广播 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="cn.jslfl.demo.dubbo.interalapi.IUserService" ref="userService" /> <!-- 和本地bean一样实现服务 --> <bean id="userService" class="cn.jslfl.demo.dubbo.service.UserServiceImpl" /> </beans> |
新建dubbo启动程序DubboServerApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 | package cn.jslfl.demo.dubbo.server; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboServerApplication { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-provider.xml"}); context.start(); System.out.println("provider started..."); System.in.read();//任意键退出 } } |
新建Maven工程dubbo_demo_client
依赖同dubbo_demo_service相同
pom.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <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> <parent> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_root</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>dubbo_demo_client</artifactId> <packaging>war</packaging> <description>dubbo consumer test</description> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 依赖接口定义 --> <dependency> <groupId>dubbo_demo</groupId> <artifactId>dubbo_demo_internalapi</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 依赖dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!--zookeeper服务--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <!--zookeeper客户端--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.6.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project> |
resources下新建dubbo consumer配置文件spring-dubbo-consumer.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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-consumer-demo" /> <!-- 注册中心服务地址,使用zookeepert广播 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- consumer配置 --> <!-- 生成远程服务代理,可以像使用本地bean一样使用service --> <dubbo:reference id="userService" interface="cn.jslfl.demo.dubbo.interalapi.IUserService" /> </beans> |
新建测试类UserClientTest,通过消费者远程调用提供者服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package cn.jslfl.demo.dubbo.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.jslfl.demo.dubbo.entity.User; import cn.jslfl.demo.dubbo.interalapi.IUserService; /** * 非WEB应用方式调用测试 * * @author * */ public class UserClientTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"spring-dubbo-consumer.xml"}); IUserService demoService = (IUserService) context.getBean("userService"); // System.out.println(demoService.hello("张三")); // User user = new User(); user.setAge(22); user.setUserName("李四"); System.out.println(demoService.getUserInfo(user)); // } } |
至此,提供者与消费者开发完成
提供者与消费者也可以通过web容器如tomcat来启动,只需在web.xml中启动spring时分别加载上spring-dubbo-provider.xml、spring-dubbo-consumer.xml即可。
dubbo_demo_api工程src\main\webapp\WEB-INF\web.xml内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!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>dubbo demo Web App</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-dubbo-provider.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app> |
dubbo_demo_client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!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>dubbo demo Client Web</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-dubbo-consumer.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>UserTestServlet</servlet-name> <servlet-class>cn.jslfl.demo.dubbo.servlet.UserTestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserTestServlet</servlet-name> <url-pattern>/user</url-pattern> </servlet-mapping> </web-app> |
创建UserTestServlet进行消费者调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package cn.jslfl.demo.dubbo.servlet; import java.io.IOException; import java.io.Writer; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.jslfl.demo.dubbo.entity.User; import cn.jslfl.demo.dubbo.interalapi.IUserService; /** * 普通WEB方式调用 * @author * */ public class UserTestServlet extends HttpServlet{ private static final long serialVersionUID = 8537346452515534864L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext()); IUserService demoService = (IUserService) context.getBean("userService"); // System.out.println(demoService.hello("张三")); // User user = new User(); user.setAge(22); user.setUserName("李四"); resp.setCharacterEncoding("GBK"); Writer out = resp.getWriter(); out.write(demoService.getUserInfo(user)); // out.flush(); out.close(); } } |
把dubbo_demo_api和dubbo_demo_client发布到tomcat中并启动服务器
源码下载
maven工程导入dubbo_demo_root
链接:http://pan.baidu.com/s/1slA9JQ1 密码:mppb
Comments are currently closed.