先来说说为什么要从传统MVC架构转为SOA架构?

MVC大家都熟悉,那SOA呢,SOA指的是面向服务架构,项目需求扩大,项目模块越来越多,传统的MVC就显出短板了,代码臃肿,效率低下,一旦瘫痪整个项目将不能使用,

而SOA架构则是基于面向服务架构,也就是把传统的MVC架构模块化服务化,比如原有的有(用户模块,订单模块,商品模块等等),那么采用SOA服务化则建这几个模块变为一个个小项目提供服务,web端向这几个服务请求则可以,这样下来团队协作变得清楚有序,代码也显得清晰,效率也就上来了,就算有一个服务瘫痪,其他的服务也不受影响。

废话不多说,凡事从helloword开始

下面先来个简单的demo,我直接从官网down下来一个例子

SOA架构和BS_xml

下载下来解压即可;

1.打开myeclipse,导入maven项目:我们只需开demo这个模块就好了

SOA架构和BS_spring_02

2.dubbo-demo-api提供的是接口,这里就一个简单的demoService:

public interface DemoService {

    String sayHello(String name);

}

3.dubbo-demo-provider是服务提供者模块,也就是对api的实现:

SOA架构和BS_ide_03


3.1provider.java我稍微做了下修改

public class Provider {

    public static void main(String[] args) throws Exception {
    	com.alibaba.dubbo.container.Main.main(args);
    }

}

说明:这个com.alibaba.dubbo.container.Main.main(args);默认加载的是META-INF/spring下的配置文件

3.2demoServiceImpl.java实现了接口:

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }

}

3.3dubbo-demo-provider.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="demo-provider"/>

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

3.4运行provider.java即可启动服务

SOA架构和BS_xml_04

3.5还有一种配置方式就是直接写在dubbo.properties文件中:

dubbo.container=log4j,spring
dubbo.application.name=demo-provider
dubbo.application.owner=
dubbo.registry.address=multicast://224.5.6.7:1234
#dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.monitor.protocol=registry
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

说明:把该文件直接放在src/main/resources目录下即可,这样启动就可以加载到了。前面说到com.alibaba.dubbo.container.Main.main(args);默认加载的是META-INF/spring下的配置文件,如果想修改,可以在dubbo.properties的文件中添加这么一句dubbo.spring.config=classpath*:(dubbo-demo-provider.xm的路径l路径)


这样的话我们的dubbo-demo-provider.xml配置文件修改为以下这样(更加简洁了):

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

此时服务提供者的项目结构具体是这样的:

SOA架构和BS_ide_05

4.dubbo-demo-consumer是服务消费者模块,也就是对api的调用:

SOA架构和BS_SOA架构和BS_06


4.1dubbo-demo-consumer.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>

</beans>

4.2dubbo.properties:

dubbo.container=log4j,spring
dubbo.application.name=demo-consumer
dubbo.application.owner=
dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.monitor.protocol=registry
dubbo.log4j.file=logs/dubbo-demo-consumer.log
dubbo.log4j.level=WARN

4.3Consumer.java:

public class Consumer {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();

        DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法
        System.out.println("====");
        System.out.println(hello); // 显示调用结果
    }
}

4.4ok!启动provider,然后运行consumer

SOA架构和BS_SOA架构和BS_07




5.后面一篇将写到把我们已有的MVC架构项目改为SOA架构的项目