Dubbo 03: 直连式 + 接口工程
- 进一步改正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文件
4.0.0 com.example.dubbo o3-link-interface 1.0.0 jar maven-compiler-plugin 3.1 1.8 1.8 -
实体类:注意要实现序列化接口,数据需要通过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文件:要引入接口工程的依赖,知道要对哪些待提供的服务进行实现
4.0.0 com.example.dubbo o4-link-userservice-provider war 1.0.0 org.springframework spring-context 4.3.16.RELEASE org.springframework spring-webmvc 4.3.16.RELEASE com.alibaba dubbo 2.6.2 com.example.dubbo o3-link-interface 1.0.0 maven-compiler-plugin 3.1 1.8 1.8 -
dubbo的服务提供者的配置文件
-
web.xml
contextConfigLocation classpath:dubbo-link-userservice-provider.xml org.springframework.web.context.ContextLoaderListener -
提供的服务实现
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文件:要引入接口工程的依赖,知道可以申请哪些服务
4.0.0 com.example.dubbo o5-link-consumer war 1.0.0 org.springframework spring-context 4.3.16.RELEASE org.springframework spring-webmvc 4.3.16.RELEASE com.alibaba dubbo 2.6.2 com.example.dubbo o3-link-interface 1.0.0 maven-compiler-plugin 3.1 1.8 1.8 -
dubbo里消费者的配置文件
-
spring核心配置文件
-
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
dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:application.xml classpath:dubbo-link-consumer.xml dispatcherServlet *.do -
返回给前端的响应页面:userDetail.jsp
用户详情页 用户id:${user.id}用户名:${user.name}用户年龄:${user.age}用户数量:${userCount}
测试
-
将服务提供者工程和消费者工程部署到tomcat上并运行
-
运行结果
分析
- 优点:
- 在直连式的基础上引入了接口工程,其中包含实体类和待提供的服务的接口,定义了可以提供哪些服务
- 服务者工程只要在其pom文件中引入对上述接口工程的依赖,对待提供的服务进行实现即可
- 消费者工程只要在其pom文件中引入对上述接口工程的依赖,对所提供的服务进行申请访问即可
- 上述接口工程的使用很好的隔离了服务消费者和服务提供者之间的耦合,在二者之间搭建了一个沟通调用的桥梁
- 缺点:
- 当提供的服务较多时,对服务者提供的服务以及消费者可以申请的服务不太好管理,无法对现有服务种类进行很好的统计与管理
文章来源于互联网:Dubbo 03: 直连式 + 接口工程
THE END
二维码