千家信息网

duboo的使用分析

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,今天就跟大家聊聊有关duboo的使用分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。一、背景随着互联网的发展,网站应用的规模不断扩大,常规的
千家信息网最后更新 2025年02月02日duboo的使用分析

今天就跟大家聊聊有关duboo的使用分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一、背景

随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行。

当越来越的的接口与实现类的增加后,duboo的xml配置会越来越多,为了防止几百几千行的代码,减少开发人员配置xml的工作量,使用duboo的注解模式,减少配置多出问题多的可能性!

二、Dubbo使用案例

Duboo注解

接口类项目:DubboServiceInterface

仅仅是一个接口类项目!接口是普通接口!

注意:将接口类项目打包成jar分别放入服务端项目跟客户端项目!

服务端项目:DubboServiceProvider

实现类fooserviceImpl.java

package com.alibaba.dubbo.demo.imp;

import com.alibaba.dubbo.config.annotation.Service;

import com.alibaba.dubbo.demo.DemoService;

@Service(version="1.0")

public class FooServiceImpl implements DemoService {

@Override

public String sayHello(String name) {

return "Hello " + name;

}

}

web.xml 配置扫描内容

DubboServiceProvider

spring org.springframework.web.servlet.DispatcherServlet

1

spring

*

org.springframework.web.context.ContextLoaderListener

contextConfigLocation classpath*:applicationContext.xml

applicationContext.xml 配置

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">

测试类Provider

package com.alibaba.dubbo.test;

import java.io.IOException;

import org.junit.Before;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

@Before

public void setUp() throws Exception {

}

@Test

public void testMain() throws IOException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

new String[] { "applicationContext.xml" });

context.start();

System.in.read();// 按任意键退出

}

}

lib下的jar包

客户端项目:DubboServiceConsumer

web.xml 配置扫描内容

applicationContext.xml文件

xmlns:context="http://www.springframework.org/schema/context"

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">

测试类:Consumer

package com.unj.dubbotest.action;

import java.io.IOException;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.config.annotation.Reference;

import com.alibaba.dubbo.demo.DemoService;

public class Consumer{

@Reference(version = "1.0")

private DemoService demoService;

@Test

public void mainTest() throws IOException {

ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(

new String[] {"applicationContext.xml"});

context.start();

demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理

String hello = demoService.sayHello("world"); // 执行远程方法

System.out.println( hello ); // 显示调用结果

}

}

lib下的jar包

看完上述内容,你们对duboo的使用分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0