1:创建dubbo-server maven工程
2:分别在dubbo-server下创建2个模块server-api(定义接口)和server-provider(实现接口)
server-api:
package com.cn.dubbo; public interface IGpHello { String sayHello(String msg); } server-provider:
package com.cn.dubbo; public class GpHelloImpl implements IGpHello { public String sayHello(String msg) { return "hello yulong "+msg; }}
package com.cn.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Bootstrap { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("META-INF/spring/dubbo-server.xml"); context.start(); System.out.println("服务启动成功"); System.in.read();//阻塞进程 }}server-provider pom.xml 引入dubbo等jar包
<?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.cn.dubbo</groupId> <artifactId>dubbo-client</artifactId> <version>1.0-SNAPSHOT</version> <url>http://www.example.com</url> <dependencies> <dependency> <groupId>com.cn.dubbo</groupId> <artifactId>server-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> </dependency> </dependencies> </project> dubbo-server.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: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="dubbo-server" owner="yulong" /> <!--注册中心--> <dubbo:registry address="N/A"/> <dubbo:service interface="com.cn.dubbo.IGpHello" ref="gpHelloService"/> <bean id="gpHelloService" class="com.cn.dubbo.GpHelloImpl" /> </beans>
3:idea工具mavenprojects 把dubbo-server打成jar包生成到本地,一会客户端引用jar4:新建dubbo-client工程5:bubbo-client-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.cn.dubbo</groupId> <artifactId>dubbo-client</artifactId> <version>1.0-SNAPSHOT</version> <url>http://www.example.com</url> <dependencies> <dependency> <groupId>com.cn.dubbo</groupId> <artifactId>server-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> </dependency> </dependencies> </project> 6:dubbo-client.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: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="dubbo-client" owner="yulong" /> <!--注册中心--> <dubbo:registry address="N/A"/> <dubbo:reference id="helloService" interface="com.cn.dubbo.IGpHello" url="dubbo://127.0.0.1:20880/com.cn.dubbo.IGpHello" /> </beans>
package com.cn.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("dubbo-client.xml"); IGpHello iGpHello = (IGpHello) context.getBean("helloService"); System.out.println(iGpHello.sayHello("yulong")); }}控制台:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello yulong yulong
Process finished with exit code 0
原文地址:https://www.cnblogs.com/Yulong123/p/10037099.html
时间: 2024-11-09 19:35:05