目录
传统工程为什么改造
引用官方文档的说明:
改造思路
对于一个MVC开发模式的项目,我们需要将CV层放在一个项目中,而M单独放在一个项目中.中间接口约定需要一个单独的项目打成jar包形式分别引入.
一个简单的例子(省略了DB相关操作):
UserService.java
public interface UserService {
public String getUser(String username);
}
UserServiceImpl.java
@Service("userFacede")
public class UserServiceImpl implements UserService{
Logger log = Logger.getLogger(UserServiceImpl.class);
public String getUser(String username) {
log.info("UserService Be Visited");
if (username.equals("milo")){
return "hello milo";
}else{
return "hello guest";
}
}
}
UserController.java
@Controller
public class UserController {
Logger log = Logger.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping("/user")
public String User(String username, HttpServletRequest request, HttpServletResponse response){
log.info("userController Be Visited");
request.setAttribute("username",userService.getUser(username));
return "user";
}
}
要改成Dubbo项目,我们需要将业务逻辑层与控制层+试图层分离,所以我们将项目改成如下样子:
- dubbodemo-parent : 父项目,定义jar包版本号,聚合所有maven项目(module)等.
- dubbodemo-facede : 定义接口,这个项目是要打成jar包分别被dubbodemo-service和dubbodemo-web引用的
- dubbodemo-service : 只做逻辑实现,也就是dubbo中的生产者
dubbodemo-web : 负责页面跳转及渲染,也就是dubbo中的消费者
分别展开讲下这几个项目:
dubbodemo-facede : 这个项目是不需要引入spring框架的.定义好接口,pom.xml中jar的打包方式就可以了.
dubbodemo-service : 需要引入 dubbodemo-facede项目(module),pom.xml:
<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">
<parent>
<artifactId>dubbodemo-parent</artifactId>
<groupId>cn.milo.dubbodemo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbodemo-service</artifactId>
<packaging>war</packaging>
<name>dubbodemo-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.milo.dubbodemo</groupId>
<artifactId>dubbodemo-facede</artifactId>
<version>${dubbodemo-facede.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- Zookeeper-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<!-- Spring Begin -->
.
.
.
<!-- Spring End -->
</dependencies>
</project>
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="gw-service-user" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="47.94.81.**:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 用户服务接口 -->
<dubbo:service interface="cn.milo.dubboservice.UserService" ref="userFacede" />
</beans>
dubbodemo-web: 同样需要引入 dubbodemo-facede项目(module),pom.xml和dubbodemo-service一样
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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="edu-web-boss" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- 注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="47.94.81.162:2181" />
<!-- 用户服务接口 这里的id可以随便写哈 -->
<dubbo:reference interface="cn.milo.dubboservice.UserService" id="admin" check="false" />
</beans>