1. dubbo-api:
service:
package com.jcx.dubbo.demo.service; public interface IDemoService { String sayHello(String userName); }
2. dubbo-provider:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jcx.dubbo.demo</groupId> <artifactId>dubbo-provider</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>
service.impl:
package com.jcx.dubbo.demo.service.impl; import com.jcx.dubbo.demo.service.IDemoService; public class DemoServiceImpl implements IDemoService { public String sayHello(String userName) { return "Hello," + userName; } }
ProviderApplication:
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class ProviderApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml"); ioc.start(); System.out.println("provider start..."); System.in.read(); } }
provider.xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" 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="dubbo-demo-provider"/> <!--指定注册中心--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/> <!--指定通信规则(通信协议,通信端口)--> <dubbo:protocol name="dubbo" port="20880"/> <!--暴露服务 ref指向真正实现--> <dubbo:service interface="com.jcx.dubbo.demo.service.IDemoService" ref="demoServiceImpl"/> <!--服务实现--> <bean id="demoServiceImpl" class="com.jcx.dubbo.demo.service.impl.DemoServiceImpl"/> </beans>
3. dubbo-consumer:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jcx.dubbo.demo</groupId> <artifactId>dubbo-consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>
ConsumerApplication:
import com.jcx.dubbo.demo.service.IDemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class ConsumerApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml"); applicationContext.start(); IDemoService demoService = (IDemoService) applicationContext.getBean("demoService"); String hello = demoService.sayHello("jcx"); System.out.println(hello); } }
consumer.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" 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="dubbo-demo-consumer"></dubbo:application> <!--指定注册中心--> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!--生成远程服务代理接口 check:启动时是否检查--> <dubbo:reference id="demoService" interface="com.jcx.dubbo.demo.service.IDemoService" check="false"/> </beans>
注:先启动提供者,后启动消费者