spring项目增加dubbo服务结构,一个服务提供方,一个服务消费方,正常调用服务接口(xml配置和api配置)

作者: admin 分类: 环境搭建 发布时间: 2019-07-08 12:51  阅读: 37 views

在很多传统行业的大龄程序猿,可能有很多人是没有用过dubbo的(我之前是jdk1.5, struts2+jsp,5年时间雷打不动,在现有产品可用的基础上,根本想不到要升级技术,或引入新技术)。现在将一个spring项目添加dubbo框架。

 

项目服务端代码请参考:https://www.deathearth.com/912.html。按照以下步骤增加配置或代码

1. 服务端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.xsd        
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 提供方应用信息,用户计算依赖关系 -->
    <dubbo:application name = "logistics" />
    <!-- 使用注册中心暴露服务地址, 直接使用127.0.0.1:2181 我这里报错连接不上 -->
    <dubbo:registry address = "zookeeper://127.0.0.1:2181" />
    <!-- 使用dubbo协议在 20880端口暴露服务 -->
    <dubbo:protocol name = "dubbo" port = "20880" />
    <!-- 声明服务 -->
    <dubbo:service version = "1.0.0" interface="com.chl.service.BasicService" ref = "basicService" timeout = "5000"  />
    <!-- 关闭所有服务的启动时检查 (没有提供者时报错) -->
    <dubbo:consumer check="false" />
</beans>

2.客户端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.xsd        
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 提供方应用信息,用户计算依赖关系 -->
    <dubbo:application name = "logistics-consumer" />
    <!-- 使用注册中心暴露服务地址 -->
    <dubbo:registry address = "zookeeper://127.0.0.1:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="basicService" interface="com.chl.service.BasicService" version = "1.0.0"/>
    
</beans>

3. 如果服务提供方配置了version版本号,而客户端没有填写version版本号,将会报错如下:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basicService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.chl.service.BasicService. No provider available for the service com.chl.service.BasicService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=logistics-consumer&dubbo=2.5.3&interface=com.chl.service.BasicService&methods=sayHello&pid=21050&side=consumer&timestamp=1562296479095 to the consumer 192.168.20.140 use dubbo version 2.5.3
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
	at com.chl.ConsumerTest.main(ConsumerTest.java:13)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.chl.service.BasicService. No provider available for the service com.chl.service.BasicService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=logistics-consumer&dubbo=2.5.3&interface=com.chl.service.BasicService&methods=sayHello&pid=21050&side=consumer&timestamp=1562296479095 to the consumer 192.168.20.140 use dubbo version 2.5.3
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
	... 6 more
注:版本号这里可以用来升级接口,当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。
可以按照以下的步骤进行版本迁移:
  1. 在低压力时间段,先升级一半提供者为新版本
  2. 再将所有消费者升级为新版本
  3. 然后将剩下的一半提供者升级为新版本

客户端加载spring相关配置测试类如下:

package com.chl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chl.service.BasicService;

public class ConsumerTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/spring.xml");
		context.start();
		BasicService bs = (BasicService)context.getBean("basicService");
		System.out.println(bs.sayHello("nono", "亚克西"));
	}
}

输出结果如下:

正常输出结果:
来自nono的问候:亚克西

dubbo通过API方式提供服务于消费服务

api注册服务如下:

package com.chl.test;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.chl.service.BasicService;
import com.chl.service.impl.BasicServiceImpl;

/**
 * 通过api的方式注册dubbo服务
 * @author chenhailong
 */
public class DubboRegistryApi {
	
	public void reg() {
		BasicService bs = new BasicServiceImpl();
		
		//应用配置
		ApplicationConfig app = new ApplicationConfig();
		app.setName("provider");

		// 注册中西配置
		RegistryConfig rc = new RegistryConfig();
		rc.setAddress("127.0.0.1:2181");
		rc.setUsername("pro");
		rc.setPassword("vider");
		
		// 服务提供方全局超时配置
		ProviderConfig pc = new ProviderConfig();
		pc.setTimeout(5 * 1000);
		
		// 服务提供者协议配置
		ProtocolConfig protocol = new ProtocolConfig();
		protocol.setPort(20881);
		protocol.setName("dubb");
		protocol.setThreads(200);
		
		// 服务提供者暴露服务配置
		ServiceConfig<BasicService> sc = new ServiceConfig<BasicService>();
		sc.setApplication(app);
		sc.setRegistry(rc);
		sc.setProvider(pc);
		sc.setProtocol(protocol);
		
		sc.setInterface(bs.getClass());
		sc.setRef(bs);
		sc.setVersion("1.0.0");
		// 暴露及注册服务
		sc.export();
	}
}

api消费代码

package com.chl.test;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.chl.service.BasicService;
/**
 * 通过api的方式注册dubbo服务
 * @author chenhailong
 */
public class DubboConsumerApi {

	public void test() {
		//应用配置
		ApplicationConfig app = new ApplicationConfig();
		app.setName("provider");

		// 注册中西配置
		RegistryConfig rc = new RegistryConfig();
		rc.setAddress("127.0.0.1:2181");
		rc.setUsername("pro");
		rc.setPassword("vider");
		
		// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
		// 服务消费
		ReferenceConfig<BasicService> ref = new ReferenceConfig<BasicService>();
		ref.setApplication(app);
		ref.setRegistry(rc);
		ref.setInterface(BasicService.class);
		
		//获取接口对象
		BasicService bs = ref.get();
		
	}
}

当然,dubbo服务也可以用注解方式进行配置[2.6.3及以上版本支持]:
http://dubbo.apache.org/zh-cn/docs/user/configuration/annotation.html

当整个dubbo服务框架的基本流程正常之后,可以进一步优化,将公用配置信息提取到外部properties文件中,方便配置的统一化管理。


   原创文章,转载请标明本文链接: spring项目增加dubbo服务结构,一个服务提供方,一个服务消费方,正常调用服务接口(xml配置和api配置)

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读