其他分享
首页 > 其他分享> > dubbo实战01-入门案例

dubbo实战01-入门案例

作者:互联网

学习一门新技术的时候,我们不仅要学会怎样运用这门技术,同时还必须明白这项技术是为了解决什么样的问题而产生的,真正做到知其然并知其所以然。

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
1、选择dubbo的注册中心:目前主流的几种注册中心主要有zookeeper、redis、Multicast、Simple
(本案列中选择zookeeper充当注册中心,版本为3.4.13)
2、dubbo的版本为2.6.2
3、jdk版本为jdk8
4、maven版本为3.5.4
5、idea为开发工具
dubbo-project
	|dubbo-provider
		|src
		  |main
		    |java
		      |com.kkcl.gmall
		        |service
		          |impl
		          	UserServiceImpl.java
		      |MainApplication.java
		    |resources
		      |provider.xml		
	|dubbo-consumer
	  |src
		  |main
		    |java
		      |com.kkcl.gmall
		        |service
		          |impl
		          	OrderServiceImpl.java
		      |MainApplication.java
		    |resources
		      |consumer.xml		
	|gmall-interface
	    |src
		  |main
		    |java
		      |com.kkcl.gmall
		        |bean
		          |UserAddress.java
		        |service
		          |UserService.java
		          |OrderService.java
本示例工程主要分为以下几个module:dubob-project为父工程;
gmall-interface、dubbo-provider、dubbo-consumer为子module,
gmall-interface主要的主要是集成接口与实体类供dubbo-provider和dubbo-consumer调用,dubbo-provider充当生产者的角色,为消费者提供服务,dubbo-consumer充当消费者的角色,
主要消费生产者提供的服务。
UserAddress.java
public class UserAddress implements Serializable {
	
	private Integer id;
    private String userAddress; //用户地址
    private String userId; //用户id
    private String consignee; //收货人
    private String phoneNum; //电话号码
    private String isDefault; //是否为默认地址    Y-是     N-否
    constructor...
    getter and setter....
}

UserService.java
public interface UserService {
	public List<UserAddress> getUserAddressList(String userId);
}

UserServiceImpl.java
public class UserServiceImpl implements UserService {
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress address1 = new UserAddress(1, "北京市", "1", "李老师", "010-56253898", "Y");
        UserAddress address2 = new UserAddress(2, "西安市)", "1", "王老师", "010-56253809", "N");
        return Arrays.asList(address1,address2);
    }
}

Order.service
public interface OrderService {
	public List<UserAddress> initOrder(String userId);
}

OrderServiceImpl.java
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    UserService userService;
    @Override
    public List<UserAddress> initOrder(String userId) {
        // TODO Auto-generated method stub
        System.out.println("用户id:"+userId);
        //1、查询用户的收货地址
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
        return addressList;
    }
}

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.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.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"/>-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!---指定通信的规则(通信协议,通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--暴露的服务-->
    <dubbo:service interface="com.kkcl.gmall.service.UserService" ref="userServiceImpl"/>
    <!--服务实现-->
    <bean id="userServiceImpl" class="com.kkcl.gmall.service.impl.UserServiceImpl"/>
</beans>

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"
       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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.kkcl.gmall.service.impl"/>
    <!--应用的名称-->
    <dubbo:application name="dubbo-consumer"/>
    <!--注册中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--声明需要调用的远程服务的名称:生成远程服务代理-->
    <dubbo:reference interface="com.kkcl.gmall.service.UserService" id="userService"/>
</beans>
MainApplication.java
public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml或者consumer.xml");
        ioc.start();
        System.in.read();
    }
}
1、到zookeeper官网下载zookeeper,这里下载的版本为3.4.13,下载完成后到zookeeper的conf目录下将zoo_simple.cfg复制重命名为zoo.cfg并配置对应的data存储的目录
dataDir=../data
2、分别启动dubbo-provider中的MainApplication.java
3、进入到dubbo的admin后台服务管理页面查看服务的启用情况
https://github.com/kkcl/kkcl-learning-example/tree/master/dubbo-project

标签:UserAddress,dubbo,01,java,入门,provider,public,String
来源: https://blog.csdn.net/qq_38061755/article/details/100180702