Dubbo : 傳統工程改造(二)

目錄

傳統工程爲什麼改造

引用官方文檔的說明:

這裏寫圖片描述

改造思路

對於一個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>  

項目源碼

http://download.csdn.net/download/shangmingtao/9983768

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章