1. 程式人生 > >集成 dubbo 微服務

集成 dubbo 微服務

end cast 微服務架構 -o 項目 zookeeper xtend protoc ren

微服務架構近年來非常的火,阿裏 的dubbo 是其中的一種解決方案。

技術分享圖片

dubbo 的微服務主要分為以下幾部分:

1.註冊中心

2.服務提供者

3.消費者

4.監控平臺

1.一般流程服務提供者向註冊中心註冊服務。

2.客戶端向註冊中心請求服務。

3.註冊中心通知客戶端訪問提供者。

4.監控負責服務是否可用。

1.註冊中心的安裝

就是安裝zookeeper ,為了測試我們可以簡單的安裝一臺就好,也可以安裝多臺做集群。

技術分享圖片

將conf 目錄下的 zoo_sample.cfg 改名成為 zoo.cfg 編輯上面兩項 就好了。

2.實現微服務的provider 。

實現 服務接口

public interface DemoService {
String sayHello(String name);
}

寫個簡單的實現

public class DemoServiceImpl implements DemoService {

@Override
public String sayHello(String name) {

return "hello:" + name;
}

}

然後做spring配置。

配置文件如下

<?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"
> <!-- 提供方應用信息,用於計算依賴關系 --> <dubbo:application name="hello-world-app" /> <!-- 使用multicast廣播註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.31.77:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="2088" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="demo.DemoService" ref="demoService" /> <!-- 和本地bean一樣實現服務 --> <bean id="demoService" class="demo.impl.DemoServiceImpl" /> </beans>

這樣provider就玩成了。

需要再平臺中引入相關的jar包。

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

3.安裝dubbo WEB 管理

包下載地址 https://github.com/apache/incubator-dubbo-ops/

將項目導入 eclipse

技術分享圖片

執行 mvn clean package 需要註意的是可能會報錯

再pom.xml 中增加

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerId>eclipse</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-compiler-eclipse</artifactId>
                        <version>2.2</version>
                    </dependency>
                </dependencies>
            </plugin>

修改配置文件

技術分享圖片

執行 java -jar dubbo-admin-0.0.1-SNAPSHOT.jar 這樣 dubbo 管理控制臺就可以運行了。

4.開發客戶端

增加配置文件

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 消費方應用名,用於計算依賴關系,不是匹配條件,不要與提供方一樣 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
    <dubbo:registry address="zookeeper://192.168.31.77:2181" />
 
    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="demoServiceClient" interface="demo.DemoService" />
</beans>

開發java代碼如下

public class DubboComsumerTest extends SimpleBaseTestCase{
    @Resource
    DemoService demoService;
    
    @Test
    public void hello(){
        String rtn=demoService.sayHello("ray");
        System.out.println(rtn);
    }
}
@RunWith(JUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-test.xml"})
public class SimpleBaseTestCase {

}

  這樣執行單元測試就可以看到效果了。

集成 dubbo 微服務