• spring + dubbo 学习


      新启动的项目中可能会使用到dubbo,因为之前并没有接触过,所以先小试一下

      示例运行环境准备:OS X 10.10.5 + java version "1.8.0_40"

      zookeeper 和 dubbo-admin运行在虚拟机上,环境为Centos7.0 + java version "1.8.0_73"

      项目使用gradlle管理

                  

      其中dubbo-demo未项目,里面新建了两个module,api为服务提供方,console为服务消费方。注意,需要在console module中添加api 的module依赖

      api中代码如下:

      DemoService

    package com.dh.demo.service;
    
    public interface DemoService {
        public String hello(String name);
    }

      DemoServiceImpl

    package com.dh.demo.service.impl;
    
    import com.dh.demo.service.DemoService;
    
    public class DemoServiceImpl implements DemoService {
        public String hello(String name) {
            return "hello " + name;
        }
    }

    服务端spring配置文件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="dubbo-demo-api"/>
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://vm:2181"/>
    
        <bean id="demoService" class="com.dh.demo.service.impl.DemoServiceImpl"/>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.dh.demo.service.DemoService" ref="demoService"/>
    
    </beans>

    启动api的测试代码如下:

    package com.dh.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    public class Provider {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/api/src/resources/dubbo-provider.xml");
            context.start();
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

      服务提供者的配置到此结束。

      下面是消费者的配置和代码

      spring配置文件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="dubbo-demo-console"></dubbo:application>
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://vm:2181"/>
    
        <!-- 要引用的服务 -->
        <dubbo:reference interface="com.dh.demo.service.DemoService" id="demoService"></dubbo:reference>
    </beans>

      启动消费者测试的代码如下

      Consumer:

    package com.dh.test;
    
    import com.dh.demo.service.DemoService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    public class Consumer {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/console/src/resources/dubbo-consumer.xml");
            context.start();
            DemoService testRegistryService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
            String hello = testRegistryService.hello("world");
            System.out.println(hello);
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

      先运行提供者测试代码,再运行消费者代码。可以看到下面输出  

      

      至此测试成功

      如果运行了dubbo-admin的话可以进去后可以查看和管理服务

      这是提供者:

      

      消费者:

      

       有些童鞋可能不知道怎么运行dubbo-demo

     看dubbo的github主页就行

     如果再jdk8的环境下运行可能在启动的时候会遇到如下问题

    Bean property 'URIType' is not writable or has 。。。。。后面省略

     解决方式参考:https://github.com/alibaba/dubbo/issues/50 中 stirp 的方式即可 

     注:dubbo-demo的build.gradle内容如下

    group 'com.dh'
    version '1.0-SNAPSHOT'
    
    apply plugin: 'java'
    
    sourceCompatibility = 1.5
    
    
    repositories {
    
        maven { url "http://repo.maven.apache.org/maven2" }
    }
    dependencies {
        compile(group: 'com.alibaba', name: 'dubbo', version: '2.5.3') {
            exclude(module: 'spring')
        }
        compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.6'
        compile group: 'com.google.zxing', name: 'core', version: '3.1.0'
        compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
        compile group: 'io.netty', name: 'netty', version: '4.0.0.Alpha8'
        compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
        compile group: 'org.springframework', name: 'spring-context-support', version: '4.1.3.RELEASE'
        compile group: 'org.springframework', name: 'spring-web', version: '4.1.3.RELEASE'
        compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.3.RELEASE'
        compile group: 'com.alibaba', name: 'fastjson', version: '1.2.7'
        compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.5'
        compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.6.3'
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.3'
        compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.2'
        compile group: 'org.jdom', name: 'jdom', version: '1.1.3'
        compile group: 'com.github.sgroschupf', name: 'zkclient', version: '0.1'
    }

      

  • 相关阅读:
    angular多个控制器如何共享数据
    $q服务的使用
    $sanitize和$sce服务的使用方法
    json对象与json字符串互转方法
    css语法和JS语法的对比
    让低版本IE也能正常运行HTML5+CSS3网站的3种解决方案
    HTML中的IE条件注释
    区块链是什么,如何评价区块链
    编写Python脚本把sqlAlchemy对象转换成dict的教程
    编写Python脚本把sqlAlchemy对象转换成dict的教程
  • 原文地址:https://www.cnblogs.com/modprobe/p/5412400.html
Copyright © 2020-2023  润新知