标签:style http io ar color os 使用 sp java
public interface CustomerService { public String getName(); }2、在服务提供方实现接口:(对服务消费方隐藏实现)
public class CustomerServiceImpl implements CustomerService{ @Override public String getName() { System.out.print("我打印"); return "打印结果"; } }3、然后引入dubbo的几个包
<?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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.jinbin.service.customer.CustomerServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="xixi_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://localhost:1234" />--> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.1.3:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.jinbin.service.customer.CustomerService" ref="demoService" /> </beans>
public class DubooProvider { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"applicationProvider.xml"}); context.start(); System.out.println("Press any key to exit."); try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?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="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.1.3:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.jinbin.service.customer.CustomerService" />
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application.xml /WEB-INF/applicationConsumer.xml</param-value>
</context-param>
@Autowired CustomerService demoService ;
@RequestMapping(value="duboo1")
public void duboo1(){
demoService.getName();
}
vi
webapps/ROOT/WEB-INF/dubbo.properties |
dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest |
启动:
./bin/startup.sh |
标签:style http io ar color os 使用 sp java
原文地址:http://www.cnblogs.com/steven9801/p/4131778.html