1. 程式人生 > >dubbo初探一之hello world

dubbo初探一之hello world

dubbo

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