最全最简单的dubbo教程-以XML的形式整合dubbo《二》

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingyangV587/article/details/84986577

目前官网给出了,四种整合dubbo的方式分别是,XML配置、属性配置、API配置、注解配置,看官网写的api跟注解配置的方式是类似的,所以我这里只写三种配置方式。详细说明见dubbo官网,这里就不再多说了。但是建议开发中用XML的形式,其它形式最好作为了解。XML整合的案例在快速入门中已经有了,只不过不是与springboot方式整合的,所以这里再次整合一遍。注意服务启动前需要启动注册中心zookeeper,且不要关闭,不知道的请参考:Zookeeper注册中心的搭建-windows单机《一》重复的后面的都不做陈述了。

开始搭建

项目结构如图:

在这里插入图片描述

共同需要的依赖

    <dependency>
		<groupId>com.alibaba.boot</groupId>
		<artifactId>dubbo-spring-boot-starter</artifactId>
		<version>0.2.0</version>
</dependency>
<dependency>
	   <groupId>com.gjy.dubbo</groupId>
	    <artifactId>dubbo-xml-springboot-api</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
</dependency>

共同接口的定义

package com.gjy.service;
public interface DefaultApiService {
	String defaultMethod(String str);
}

服务生产者

项目结构

在这里插入图片描述

服务生产者

生产者实现接口

package com.gjy.server;
import org.springframework.stereotype.Service;
import com.gjy.service.DefaultApiService;
@Service("defaultService")
public class DefaultServiceImpl implements DefaultApiService{
	
	public String defaultMethod(String str) {
		return "hello "+str;
	}
}

声明相关配置信息

在provider中声明注册中心地址,服务名称,使用的协议等。

<?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-xml-spring-boot-provider" />

	<!-- 用于配置连接注册中心相关信息 -->
	<dubbo:registry address="zookeeper://localhost:2181" dynamic="false" />
	<!-- 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 -->
	<dubbo:protocol name="dubbo" port="20880" accesslog="true" />

	<!--用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 -->
	<!--每个接口都应定义版本号,为后续不兼容升级提供可能 -->
	<!--ref:服务的真正实现类 -->
	<dubbo:service interface="com.gjy.service.DefaultApiService" ref="defaultService" version="1.0.0"  />

	<!--监控中心配置 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。 -->
	<!--<dubbo:monitor protocol="registry"></dubbo:monitor>-->
	<!-- 直连监控中心服务器地址 -->
	<!-- <dubbo:monitor address="localhost:6379"></dubbo:monitor> -->

</beans>

服务启动

注意包扫描,与加载provider.xml文件

package com.gjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

/**
 * @ClassName:  DubboProviderApplication   
 * @Description:服务提供者启动类  
 */
@SpringBootApplication
@ComponentScan(basePackages={"com.gjy"})
@ImportResource(locations="classpath:provider.xml")
public class DubboProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(DubboProviderApplication.class, args); 
	}

}

服务消费者

服务注册

在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.xsd 
     http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	 
	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="dubbo-xml-spring-boot-consumer" />

	<dubbo:registry address="zookeeper://localhost:2181"/>

	<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
	<!-- version 版本号必须和服务提供者版本号对应 -->
	<!--interface 引入服务提供者对外暴露的接口  -->
	<!--url:服务消费者直接服务提供者,服务提供者地址  -->
	<dubbo:reference id="defaultService" interface="com.gjy.service.DefaultApiService"  version="1.0.0" retries="3"/>

	<!--监控中心配置 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。 -->
	<!--<dubbo:monitor protocol="registry"></dubbo:monitor>-->
	<!--关闭所有消费者启动时检查   -->
	<!--<dubbo:consumer check="false"></dubbo:consumer>-->

</beans>

服务启动

注意包扫描与配置文件的加载

package com.gjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

/**
 * 
 * @ClassName:  DubboConsumerApplication   
 * @Description:消费者
 *
 */
@SpringBootApplication
@ComponentScan(basePackages={"com.gjy"})
@ImportResource(locations = "classpath:consumer.xml")
public class DubboConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(DubboConsumerApplication.class, args);
	}

}

调用服务

服务调用的代码为:

package com.gjy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gjy.service.DefaultApiService;

@RestController
public class DemoConsumerController {
	
	@Autowired
	private DefaultApiService defaultService; 
	
	@RequestMapping("/sayHello")
	public String sayHello(@RequestParam String name) {
		return defaultService.defaultMethod(name);
	}

}

调用结果测试

访问:http://localhost:8080/sayHello?name=hai
如果出现如图说明ok了:
在这里插入图片描述

参考博客:https://blog.csdn.net/niugang0920/article/details/82316399

猜你喜欢

转载自blog.csdn.net/jingyangV587/article/details/84986577