dubbo Demo

雖然dubbo 已經不更新了,A公司已一直在用HSF,潮流追隨者也在使用spring cloud,最近須要用dubbo,發一貼dubbo Demo.html

1. 安裝zookeeper ,前面的博中已有介紹,略去。spring

2. 建服務端apache

/**
 * Created by on 2017/7/26.
 */
public interface CenterService {
    List<String> getPermissions(Long id);
}
/**
 * Created by on 2017/7/26.
 */
public class CenterServiceImpl implements CenterService {
    public List<String> getPermissions(Long id) {
        List<String> demo = new ArrayList<String>();
        demo.add(String.format("The left Id is :%d", id - 1));
        demo.add(String.format("This Id is :%d", id));
        demo.add(String.format("The next Id is:%d", id + 1));
        return demo;
    }

3. providerapi

/**
 * Created by on 2017/7/26.
 */
public class DubboProvider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("服務已經啓動...");
        System.in.read();

    }

spring 配置文件 dubbo-provider.xmltomcat

<?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-admin 或 dubbo-monitor 會顯示這個名字,方便辨識-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 註冊中心暴露服務,注意要先開啓 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 協議實現定義好的 api.CenterService 接口-->
    <dubbo:service interface="com.dennis.dubbo.provider.CenterService" ref="centerService" protocol="dubbo" />
    <!--具體實現該接口的 bean-->
    <bean id="centerService" class="com.dennis.dubbo.provider.impl.CenterServiceImpl"/>
</beans>

3. 建客戶端app

/**
 * Created by on 2017/7/26.
 */
public class ConsumerService {


        public static void main(String[] args) {
        //測試常規服務
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        context.start();
        System.out.println("consumer start");
        //    DemoServiceImpl de=new DemoServiceImpl();
            CenterService centerService = context.getBean(CenterService.class);
        System.out.println("consumer");
        System.out.println(centerService.getPermissions(1L));
    }

}

spring 配置 dubbo-consumer.xmlide

<?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="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 協議調用定義好的 api.CenterService 接口-->

    <dubbo:reference id="centerService"
           interface="com.dennis.dubbo.provider.CenterService" />

</beans>
 

客戶端 沒有CenterService 會報錯的,我在客戶端建了一個同名接口測試

/**
 * Created by on 2017/7/26.
 */
public interface CenterService {
    List<String> getPermissions(Long id);
}

4.啓動zookeeperspa

啓動服務端:code

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.
org.springframework.context.support.ClassPathXmlApplicationContext@3fa77460: here
服務已經啓動...

啓動客戶端:

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.
consumer start
consumer
[The left Id is :0, This Id is :1, The next Id is:2]

5.驗證

時間有限 ,沒有在tomcat下加 duboo-admin

打開zookeeper的log文件, 裏面對provider consumer進行了計數,應該是OK的。

隨便一搞,若有不正確的地方請你們批評指正。

相關文章
相關標籤/搜索