dubbo初探一之hello world

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo初探一之hello world相关的知识,希望对你有一定的参考价值。

pom.xml

<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.7</version>
    </dependency>
</dependencies>


GreetingsService.java

package service;

public interface GreetingsService {
    String sayHi(String name);
}


GreetingsServiceImpl.java

package service.impl;

import service.GreetingsService;

public class GreetingsServiceImpl implements GreetingsService {

    @Override
    public String sayHi(String name) {
        return "hi, " + name;
    }
}


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="demo-provider"/>
       <dubbo:registry address="multicast://224.5.6.7:1234"/>
       <dubbo:protocol name="dubbo" port="20880"/>
       <dubbo:service interface="service.GreetingsService" ref="greetingsService"/>
       <bean id="greetingsService" class="service.impl.GreetingsServiceImpl"/>
</beans>


consumer.xml

<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="demo-consumer"/>
       <dubbo:registry address="multicast://224.5.6.7:1234"/>
       <dubbo:reference id="greetingsService" interface="service.GreetingsService"/>
</beans>


测试代码

Provider.java

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});

        context.start();

        System.in.read();
    }
}


Consumer.java

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.GreetingsService;

public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});

        context.start();

        GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");

        String hello = greetingsService.sayHi("world");

        System.out.println(hello);
    }
}


以上是关于dubbo初探一之hello world的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMq初探——Hello World

node初探(很全的hello world工程)

3.Dubbo2.5.3快速启动Hello World

Dubbo框架的Hello World

Flask初探二( app.route 内部实现)

又见“Hello World”,第一个Netty示例!