有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步, 认准 https://blog.zysicyj.top
全网最细面试题手册,支持艾宾浩斯记忆法。这是一份最全面、最详细、最高质量的 java 面试题,不建议你死记硬背,只要每天复习一遍,有个大概印象就行了。https://store.amazingmemo.com/chapterDetail/1685324709017001`
快速开始使用 Dubbo
Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展 进行加载。
如果不想使用 Spring 配置,可以通过 API 的方式 进行调用。
服务提供者
完整安装步骤,请参见:示例提供者安装
定义服务接口
DemoService.java 1:
1 2 3 4 5 6
| package org.apache.dubbo.demo;
public interface DemoService { String sayHello(String name); }
|
在服务提供方实现接口
DemoServiceImpl.java 2:
1 2 3 4 5 6 7 8 9 10 11
| package org.apache.dubbo.demo.provider; import org.apache.dubbo.demo.DemoService; public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hello " + name; } }
|
用 Spring 配置声明暴露服务
provider.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="hello-world-app" /> <dubbo:registry address="multicast://224.5.6.7:1234" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" /> <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" /> </beans>
|
加载 Spring 配置
Provider.java:
1 2 3 4 5 6 7 8 9 10
| import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"}); context.start(); System.in.read(); } }
|
服务消费者
完整安装步骤,请参见:示例消费者安装
通过 Spring 配置引用远程服务
consumer.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <dubbo:application name="consumer-of-helloworld-app" /> <dubbo:registry address="multicast://224.5.6.7:1234" /> <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" /> </beans>
|
加载 Spring 配置,并调用远程服务
Consumer.java 3:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import org.springframework.context.support.ClassPathXmlApplicationContext; import org.apache.dubbo.demo.DemoService; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/dubbo-demo-consumer.xml"}); context.start(); DemoService demoService = (DemoService)context.getBean("demoService"); String hello = demoService.sayHello("world"); System.out.println(hello); } }
|