Dubbo(六)--dubbo-helloworld工程改造

一、将服务提供者注册到注册中心(暴露服务)

1.1 导入dubbo依赖,导入操作zookeeper的客户端(curator)

1.2 配置服务提供者

创建spring配置文件provider.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 1.指定当前服务/应用的名字(同样的服务名相同,不要和别的服务同名) -->
	<dubbo:application name="user-service-provider" />
	<!-- 2.指定注册中心的位置 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 3.指定通信規則(通信协议?通信端口) -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 4.暴露服务 ref:指向服务的真正的实现对象 -->
	<dubbo:service interface="com.atguigu.gmall.service.UserService"
		ref="userServiceImpl" />
	<!-- 服务的实现类 -->
	<bean id="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl" />
</beans>

1.3 测试类:

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {

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

效果:

二、服务的消费者从注册中心订阅

2.1 导入dubbo依赖,导入操作zookeeper的客户端(curator)

2.2 配置服务消费者

创建spring配置文件consumer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 1.指定当前服务/应用的名字(同样的服务名相同,不要和别的服务同名) -->
	<dubbo:application name="order-service-comsumer" />
	<!-- 2.指定注册中心的位置 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<!-- 生命需要调用的远程服务的接口:生成远程服务代理 -->
	<dubbo:reference interface="com.atguigu.gmall.service.UserService"
		id="userService"></dubbo:reference>

	<context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan>
</beans>

2.3 测试类

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.gmall.service.OrderService;

public class MainApplication {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("consumer.xml");
		OrderService orderService = ac.getBean(OrderService.class);
		orderService.initOrder("kenneth");
		
		System.in.read();
	}
}

效果:

猜你喜欢

转载自blog.csdn.net/csdn_kenneth/article/details/82527575