您的位置:首页 > 其它

dubbo初探一之hello world

2018-02-27 09:37 489 查看
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