dubbo——回聲測試
阿新 • • 發佈:2019-02-04
Dubbo將所有的服務都實現了一個EchoService,可以在Java程式中進行呼叫,測試服務是否正常。
spring-all.xml:
dubbo/dubbo-consumer.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 注意:使用阿里巴巴官方推薦的dubbo打包方式,必須將spring載入的檔案放到META-INF/spring/目錄下 --> <import resource="classpath:dubbo/*.xml"/> </beans>
<?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="hello-world-app" /> <dubbo:registry id="hi-registry" address="zookeeper://10.144.4.17:2181" protocol="dubbo"/> <dubbo:reference id="hiService" registry="hi-registry" interface="com.hi.service.IHiService" group="hi"></dubbo:reference> </beans>
public class Main10 { public static void main(final String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:META-INF/spring/spring-all.xml"); IHiService hiService = (IHiService) context.getBean("hiService"); EchoService echoService = (EchoService)hiService;<code>// </code><code>強制轉型為EchoService</code> String status = (String)echoService.$echo("OK");<code>// </code><code>回聲測試可用性</code> System.out.println("================="+status); } }