标签:pre pat 指定 end project system 生成 framework static
<?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-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-consumer</module> <module>dubbo-provider</module> </modules> <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>
package com.jcx.dubbo.demo.service; public interface IDemoService { String sayHello(String userName); }
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; } }
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(); } }
<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>
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); } }
<?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>
注:先启动提供者,后启动消费者
标签:pre pat 指定 end project system 生成 framework static
原文地址:https://www.cnblogs.com/s-star/p/12464753.html