• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

dubbo入门案例(xml配置)

dubbo入门案例(xml配置)

创建server-common-api项目

server-common-api中创建消费者和提供者都需要的类,实现代码复用

用户地址实体类

package com.yl.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 用户地址
 *
 * @author Y-wee
 */
@AllArgsConstructor
@Getter
@Setter
@ToString
public class UserAddress implements Serializable {
    /**
     * 主键
     */
    private Integer id;
    /**
     * 用户地址
     */
    private String userAddress;
    /**
     * 用户id
     */
    private String userId;
    /**
     * 收货人
     */
    private String consignee;
    /**
     * 电话号码
     */
    private String phoneNum;
    /**
     * 是否为默认地址:Y-是,N-否
     */
    private String isDefault;

}

订单服务接口

package com.yl.service;

import com.yl.entity.UserAddress;

import java.util.List;

/**
 * 订单
 *
 * @author Y-wee
 */
public interface OrderService {

    /**
     * 初始化订单
     *
     * @param userId
     */
    List<UserAddress> initOrder(String userId);

}

用户服务接口

package com.yl.service;

import com.yl.entity.UserAddress;

import java.util.List;


/**
 * 用户
 *
 * @author Y-wee
 */
public interface UserService {

    /**
     * 按照用户id返回所有的收货地址
     *
     * @param userId
     * @return
     */
    List<UserAddress> getUserAddressList(String userId);

}

创建server-provider项目

server-provider实现用户相关业务,作为服务提供者

导入依赖

<dependencies>
    <dependency>
        <groupId>com.yl</groupId>
        <artifactId>server-common-api</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <!-- 引入dubbo -->
    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
    <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
</dependencies>

用户业务实现类

package com.yl.user.service.impl;

import com.yl.entity.UserAddress;
import com.yl.service.UserService;

import java.util.Arrays;
import java.util.List;

/**
 * 用户
 *
 * @author Y-wee
 */
public class UserServiceImpl implements UserService {

    /**
     * 获取用户地址
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        System.out.println("---UserServiceImpl.....old...");

        UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");

		/*try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/

        return Arrays.asList(address1, address2);
    }

}

创建配置文件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="user-service-provider"></dubbo:application>
    <!--指定注册中心的位置 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    <!--指定通信规则(通信协议|通信端口)-->
    <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
    <!-- 暴露服务,ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.yl.service.UserService"
                   ref="userServiceImpl01" timeout="1000" version="1.0.0">
        <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
    </dubbo:service>
    <!--统一设置服务提供方的规则  -->
    <dubbo:provider timeout="1000"></dubbo:provider>
    <!-- 连接监控中心 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>

    <bean id="userServiceImpl01" class="com.yl.user.service.impl.UserServiceImpl"></bean>

</beans>

启动类

package com.yl.user;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {
	
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
		ioc.start();
		
		System.in.read();
	}

}

启动服务,将提供者以user-service-provider名称注册到zookeeper,可以通过dubbo-admin控制台查看

创建server-consumer项目

导入依赖,与server-provider一致

订单业务实现类

package com.yl.order.service.impl;

import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import com.yl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单
 *
 * @author Y-wee
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserService userService;

    /**
     * 调用用户服务
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> initOrder(String userId) {
        System.out.println("用户id:" + userId);

        // 查询用户的收货地址
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress());
        }
        return addressList;
    }


}

创建配置文件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://dubbo.apache.org/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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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">

	<context:component-scan base-package="com.yl.order.service.impl"></context:component-scan>

	<dubbo:application name="order-service-consumer"></dubbo:application>
	<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
	<!--声明需要调用的远程服务接口-->
	<dubbo:reference interface="com.yl.service.UserService"
		id="userService" timeout="5000" retries="3" version="*">
		<!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
	</dubbo:reference>
	<!--是否开启启动时检查提供者服务:false-关闭-->
	<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>

	<dubbo:monitor protocol="registry"></dubbo:monitor>
	<!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
	
</beans>

启动类

package com.yl.order;

import java.io.IOException;

import com.yl.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainApplication {
	
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
		OrderService orderService = applicationContext.getBean(OrderService.class);
		orderService.initOrder("1");

		System.out.println("---调用完成");
		System.in.read();
	}

}

启动服务,调用实现user-service-provider服务调用

记得快乐
posted @ 2022-04-03 21:40  Y-wee  阅读(333)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3