进一步改正dubbo框架中简单的直连式的不足
需要用到3个相互独立的maven工程,项目1为maven的java工程作为接口工程,项目2,3为maven的web工程
工程1:o3-link-interface 作为接口工程 工程2:o4-link-userservice-provider 作为服务的提供者 工程3:o5-link-consumer 作为使用服务的消费者
工程1
结构:与简单的直连式不同的是,引入了接口工程,将实体类和所提供的服务的接口放在接口工程里
pom文件
<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.example.dubbo</groupId> <artifactId>o3-link-interface</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <build> <plugins> <!--JDK1.8编译插件--> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
实体类:注意要实现序列化接口,数据需要通过socket网络传输
package com.example.dubbo.model; import java.io.Serializable; public class User implements Serializable { private String id; private String name; private String age; @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public User(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public User() { } }
服务接口:
package com.example.dubbo.service; import com.example.dubbo.model.User; public interface UserService { /** * 根据用户id,获取用信息 */ User queryUserById(String id); }
工程2
结构
pom文件:要引入接口工程的依赖,知道要对哪些待提供的服务进行实现
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>o4-link-userservice-provider</artifactId> <packaging>war</packaging> <version>1.0.0</version> <dependencies> <!-- Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.example.dubbo</groupId> <artifactId>o3-link-interface</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <!--JDK1.8编译插件--> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
dubbo的服务提供者的配置文件
<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 服务提供者标识 --> <dubbo:application name="o4-link-userservice-provider"/> <!-- 使用的协议和端口 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 提供的服务 --> <dubbo:service interface="com.example.dubbo.service.UserService" ref="userService" registry="N/A"/> <!-- 服务的实现类--> <bean id="userService" class="com.example.dubbo.service.impl.userServiceImpl"/> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo-link-userservice-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
提供的服务实现
package com.example.dubbo.service.impl; import com.example.dubbo.model.User; import com.example.dubbo.service.UserService; public class userServiceImpl implements UserService { @Override public User queryUserById(String id) { User user = new User(); user.setId(id); user.setName("橘子"); user.setAge("18"); return user; } @Override public int queryAllUserCount() { return 3; } }
工程3
结构
pom文件:要引入接口工程的依赖,知道可以申请哪些服务
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>o5-link-consumer</artifactId> <packaging>war</packaging> <version>1.0.0</version> <dependencies> <!--Spring依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!--dubbo依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.example.dubbo</groupId> <artifactId>o3-link-interface</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <!--JDK1.8编译插件--> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
dubbo里消费者的配置文件
<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 消费者标识 --> <dubbo:application name="o5-link-consumer"/> <!-- 引用的远程服务 --> <dubbo:reference id="userService" interface="com.example.dubbo.service.UserService" url="dubbo://127.0.0.1:20880" registry="N/A"/> </beans>
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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描组件 --> <context:component-scan base-package="com.example.dubbo.web.controller"/> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
Controller层
package com.example.dubbo.web.controller; import com.example.dubbo.model.User; import com.example.dubbo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController { @Autowired UserService userService; /** * 响应前端请求,返回用用户详细信息以及总的用户个数 */ @RequestMapping("/getUserDetail.do") public String getUserDetail(String id, Model model){ //获取数据 User user = userService.queryUserById(id); int userCount = userService.queryAllUserCount(); //存放数据 model.addAttribute("user", user); model.addAttribute("userCount", userCount); //跳转到用户详情页面 return "userDetail"; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml classpath:dubbo-link-consumer.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
返回给前端的响应页面:userDetail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户详情页</title> </head> <body> <div>用户id:${user.id}</div> <div>用户名:${user.name}</div> <div>用户年龄:${user.age}</div> <div>用户数量:${userCount}</div> </body> </html>
测试
将服务提供者工程和消费者工程部署到tomcat上并运行
运行结果
分析
优点:
在直连式的基础上引入了接口工程,其中包含实体类和待提供的服务的接口,定义了可以提供哪些服务
服务者工程只要在其pom文件中引入对上述接口工程的依赖,对待提供的服务进行实现即可
消费者工程只要在其pom文件中引入对上述接口工程的依赖,对所提供的服务进行申请访问即可
上述接口工程的使用很好的隔离了服务消费者和服务提供者之间的耦合,在二者之间搭建了一个沟通调用的桥梁
缺点:
当提供的服务较多时,对服务者提供的服务以及消费者可以申请的服务不太好管理,无法对现有服务种类进行很好的统计与管理
标签:
留言评论