1. 程式人生 > >dubbo系列二、dubbo+zookeeper+dubboadmin分布式服務框架搭建(windows平臺)

dubbo系列二、dubbo+zookeeper+dubboadmin分布式服務框架搭建(windows平臺)

limit send dem ring 分布 gui ref urn standard

一、zookeeper配置中心安裝

1、下載安裝包,zookeeper-3.4.6.tar.gz

2、解壓安裝包,修改配置文件

參考zookeeper-3.4.6/conf/zoo_sample.cfg文件,同步錄下建立zoo.cfg,配置如下:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit
=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=E:\項目\zookeeper-3.4.6\data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns
=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to
"0" to disable auto purge feature #autopurge.purgeInterval=1

3、啟動zk

點擊E:\項目\zookeeper-3.4.6\bin\zkServer.cmd

socket connection from /192.168.1.100:54836

二、dubboadmin監控中心的安裝配置

1、下載tomcat安裝運行

2、下載dubbo-admin-2.5.8.war到tomcat7 \ webapps目錄下

3、修改dubbo.properties

重啟tomcat、在編譯後的文件中找到\WEB-INF文件夾下的dubbo.properties文件,然後進行配置,默認屬性配置如下:

dubbo.registry.address=zookeeper://192.168.1.100:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

4、驗證dubbo-admin

重啟zk、tomcat、訪問:http://192.168.1.100:8080/dubbo-admin-2.5.8 ,進入監控中心的管理界面(默認管理員賬戶密碼為:root,root)

技術分享圖片

三、dubbo代碼示例

1、公共接口service

package com.dubbo.demo.api;

public interface DemoRpcService {

    /**
     * 測試方法
     * @return
     */
    String getUserName(String uid);
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dubbo.demo</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

2、生產者代碼

package com.dubbo.demo;
import com.dubbo.demo.api.DemoRpcService;
public class DemoRpcServiceImpl implements DemoRpcService {


    public String getUserName(String uid) {
        System.out.println("接收入參:"+uid);
        return "小明";
    }
}

啟動代碼:

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
        context.start();
        // 阻塞當前進程,否則程序會直接停止
        System.in.read();
    }

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應用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo註冊地址-->
    <dubbo:registry address="zookeeper://192.168.1.100:2181"/>

    <!--dubbo協議地址-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--接口聲明-->
    <dubbo:service interface="com.dubbo.demo.api.DemoRpcService" ref="demoRpcService"/>
    <bean id="demoRpcService" class="com.dubbo.demo.DemoRpcServiceImpl"/>
</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dubbo.demo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- zookeeper客戶端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!-- api接口 -->
        <dependency>
            <groupId>dubbo.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

3、消費者代碼

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
context.start();

String useId = "123456";
DemoRpcService demoService = (DemoRpcService) context.getBean("demoRpcService");
System.out.println("收到結果"+demoService.getUserName(useId));

// 阻塞當前進程,否則程序會直接停止
System.in.read();
}

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應用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo註冊地址-->
    <dubbo:registry address="zookeeper://192.168.1.100:2181"/>

    <!--接口引用-->
    <dubbo:reference interface="com.dubbo.demo.api.DemoRpcService" id="demoRpcService"/>
</beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dubbo.demo</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
        </dependency>
        <!-- zookeeper客戶端 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!-- api接口 -->
        <dependency>
            <groupId>dubbo.demo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

4、運行測試

先啟動生產者、再啟動消費者

Connected to the target VM, address: ‘127.0.0.1:52472‘, transport: ‘socket‘
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
接收入參:123456
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
收到結果小明

5、代碼下載地址

GitHub

dubbo系列二、dubbo+zookeeper+dubboadmin分布式服務框架搭建(windows平臺)