Dubbo 02: 直连式
直连式
-
需要用到两个相互独立的maven的web项目
项目1:o1-link-userservice-provider 作为服务的提供者 项目2:o2-link-consumer 作为使用服务的消费者
项目1
-
结构
-
pom文件
<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>o1-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> </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>
-
实体类:注意要实现序列化接口,数据需要通过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); }
-
服务接口的实现类
package com.example.dubbo.service.impl; import com.example.dubbo.model.User; import com.example.dubbo.service.UserService; public class UserServiceImpl implements UserService { /** * 根据用户id,获取用户信息 */ @Override public User queryUserById(String id) { User user = new User(); user.setId(id); user.setName("橘子"); user.setAge("21"); return user; } }
-
dubbo框架里的服务提供者所对应的配置文件:dubbo-link-userservice-provider.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="//dubbo.apache.org/schema/dubbo" xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //dubbo.apache.org/schema/dubbo //dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 对外提供的服务的名称,要具有唯一性--> <dubbo:application name="o1-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="//xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//xmlns.jcp.org/xml/ns/javaee //xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 将dubbo框架交给spring框架管理 --> <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>
注意
-
由于采用的是直连方式,要把项目1,打成jar包,后面项目2会引用这个jar包
-
项目1打成jar包的步骤
1.将项目1的pom文件中的<packaging>war</packaging>注释掉,这样打包的时候默认会打成jar包 2.点击maven项目管理中的项目1的Lifecycle里的install(操作见下图) 3.把注释放开
项目2
-
结构
-
pom文件:其中要根据项目1的坐标将之前把项目1打成的jar包引入项目2中使用
<project xmlns="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.dubbo</groupId> <artifactId>o2-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> <!-- 对01-link-userservice-provider项目的依赖--> <dependency> <groupId>com.example.dubbo</groupId> <artifactId>o1-link-userservice-provider</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框架里的消费者所对应的配置文件:dubbo-link-consumer.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="//dubbo.apache.org/schema/dubbo" xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //dubbo.apache.org/schema/dubbo //dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 消费者的唯一标识--> <dubbo:application name="o2-link-consumer"/> <!-- 引用的远程服务的相关信息 --> <dubbo:reference id="userService" interface="com.example.dubbo.service.UserService" url="dubbo://127.0.0.1:20880" registry="N/A"/> </beans>
-
spring核心配置文件:application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:context="//www.springframework.org/schema/context" xmlns:mvc="//www.springframework.org/schema/mvc" xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd //www.springframework.org/schema/context //www.springframework.org/schema/context/spring-context.xsd //www.springframework.org/schema/mvc //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层
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("/getUser.do") public String getUser(String id, Model model){ //获取数据 User user = userService.queryUserById(id); //存放数据 model.addAttribute("user", user); //返回到用户详情页面 return "userDetail"; } }
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="//xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//xmlns.jcp.org/xml/ns/javaee //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>用户标识:${user.id}</div> <div>用户名称:${user.name}</div> <div>用户年龄:${user.age}</div> </body> </html>
测试
-
将项目1(服务提供者)和项目2(消费者),分别部署到tomcat服务器上并启动服务器
-
从消费者端去访问获取服务
直连缺点
- 由于事先要把项目1打成jar包,项目2中依赖项目1的jar包,上述方式虽然实现了消费者对服务的访问,但是上述直连方式并没有实现服务提供者和消费者的真正分离,业务还是耦合在一个项目中(这里是耦合在项目2里)