赞
踩
a.新建Maven Project, 里面只有接口(dubbo-service)
RPC框架不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现.
package com.shi.api;
/**
* 接口
* @author shixiangcheng
* 2019-06-07
*/
public interface UserService {
public String sayHi(String name);
}
b.新建Maven Project, 写接口的实现类(dubbo-service-impl)
在duboo-service-impl中配置pom.xml
<dependency>
<groupId>com.shi</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
接口实现类
package com.shi.api.impl;
import com.shi.api.UserService;
/**
* 接口实现类
* @author shixiangcheng
* 2019-06-07
*/
public class UserServiceImpl implements UserService {
public String sayHi(String name) {
return "Hello "+name;
}
}
dubbo-provider.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者 --> <dubbo:application name="dubbo-provider"/> <!--注册中心配置,用于配置连接注册中心的相关信息。--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!--协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。20880--> <dubbo:protocol name="dubbo" port="20888"/> <dubbo:monitor protocol="registry"/> <!-- 和本地bean一样实现服务 --> <bean id="userService" class="com.shi.api.impl.UserServiceImpl"/> <!--服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心。--> <dubbo:service retries="0" interface="com.shi.api.UserService" ref="userService" timeout="100000"/> </beans>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="dubbo-provider.xml"/>
<!-- spring容器扫描指定包下的所有类,如果类上有注解 那么根据注解产生相应bean对象已经映射信息 -->
<context:component-scan base-package="com.shi"/>
</beans>
提供方测试类
package com.shi.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 提供方测试类
* @author shixiangcheng
* 2019-06-07
*/
public class Test {
public static void main(String [] args) throws IOException{
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();//启动Spring容器
System.in.read();
}
}
a.新建Maven Project(dubbo-consumer)
在duboo-consumer中配置pom.xml
<dependency>
<groupId>com.shi</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer"/>
<!--使用zookeeper注册中心暴露服务地址-->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!--生成远程服务代理,可以像使用本地bean一样使用服务,检查级联关系,默认为true,当有依赖服务的时候,需要根据需求进行设置-->
<dubbo:reference id="userService" check="false" interface="com.shi.api.UserService"/>
</beans>
测试类
package com.shi.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.shi.api.UserService; /** * 消费方测试类 * @author shixiangcheng * 2019-06-07 */ public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext (new String[]{"dubbo-consumer.xml"}); //@Reference从注册中心得到代理对象,通过代理对象调用真实方法 UserService userService=(UserService)context.getBean("userService"); System.out.println(userService.sayHi("World")); } }
a.开启telnet功能(下面是windows 10操作系统的截图)
b.将提供方服务启动并在ZK注册成功
c.调用接口
命令:telnet Ip 端口
命令:ls 显示服务列表
命令:invoke 调用接口
补充:若接口参数为对象:
public String sayHi(UserDTO userDTO)
则调用命令如下:
invoke com.shi.service.UserService.sayHi({"id":"11","name":"888","class":"com.shi.UserDTO"})
若接口参数为集合:
public String sayHi(List<UserDTO> userDTOList)
则调用命令如下:
invoke com.shi.service.UserService.sayHi([{"id":"11","name":"888","class":"com.shi.UserDTO"}])
欢迎大家积极留言交流学习心得,点赞的人最美丽,谢谢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。