1. 程式人生 > >基於mave的dubbo分散式架構

基於mave的dubbo分散式架構


1、抽離介面

dubbo-api工程,根據業務抽離介面,deploy到mave nexus。

public interface TestService {    /**
     * @param cmd
     * @return     */
    public String linuxCMD(String cmd);
}

提供三維座標:

<dependency>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-api</artifactId>
  <version>0.0
.1-SNAPSHOT</version> </dependency>

2、提供者和消費者並行

dubbo-service工程pom引入實現介面三維座標實現介面。

import com.dubbo.service.TestService;
import com.dubbo.common.TestLinuxCmd;
public class TestServiceImpl implements TestService {      @Override    public String linuxCMD(String cmd) {        String exec
= TestLinuxCmd.exec("192.168.20.20", "root", "123456", 22, name);        return exec;    } }

注:TestLinuxCmd方法來至dubbo-common工程,公共方法到放到這裡,就不多做解釋了。

寫完服務記得暴露出服務:

<dubbo:service interface="com.dubbo.service.TestService" ref="testService" />

dubbo-controller工程pom同樣也要引入實現介面三維座標,呼叫這個介面。

package com.dubbo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dubbo.service.TestService;
@Controller
public class MyController {    @Autowired    
   private TestService testService;    @RequestMapping(value = "/test")    @ResponseBody    
   public String testSay(@RequestParam(value = "name",defaultValue = "")String name){        StringBuffer sb = new StringBuffer();        sb.append("Dubbo: ").append(testService.sayHello(name));        
       return sb.toString();    } }

消費者不需要關心誰提供的服務,它只需要呼叫三維座標的介面即可。

寫完同樣記得暴露出服務:

    <dubbo:reference interface="com.dubbo.service.TestService" 
id="testService" check="false" />