• spring + dubbo + SSM + maven例子demo


    dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

    官网:http://dubbo.apache.org

    架构如图(来自官网):

    搭建简单的dubbo项目,步骤如下:

    一,搭建父工程,再搭建子工程。如图:

    1,新建父工程,Packing 选择pom方式

    2,新建子工程test-api

       在父工程处,右键 maven - new maven module project :

    如上,再建立其他子项目。

    3,父工程pom.xml添加jar包,如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.luo</groupId>
        <artifactId>test-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
        <build />
        <properties>
            <motan.version>0.3.0</motan.version>
            <!-- 在阿里巴巴内部广泛使用的GA版本为:2.4.9,强烈推荐此版本 -->
            <dubbo.version>2.5.3</dubbo.version>
            <dubbox.version>2.8.4</dubbox.version>
            <spring.version>4.3.6.RELEASE</spring.version>
            <java.version>1.7</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <!-- spring相关 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.6.11</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.6.11</version>
            </dependency>
        </dependencies>
        <modules>
            <module>test-api</module>
            <module>test-consumer</module>
            <module>test-provider</module>
        </modules>
    </project>

    4,api项目添加:

    package com.luo;
    
    import java.util.List;
    
    import com.luo.domain.User;
    
    public interface DemoService {
        String sayHello(String name);  
        List<User> getUser();
    }
    package com.luo.domain;
    
    import java.io.Serializable;
    
    public class User  implements Serializable {
        int id;
        String name;
        int age;
    
        public User(int id, String name, int age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }

    结构如图:

    5,provider:实现接口DemoService:

    package com.luo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.stereotype.Service;
    
    import com.luo.domain.User;
    @Service("demoService")
    public class DemoServiceImpl implements DemoService {
    
        public String sayHello(String name) {
            return name;
        }
    
        public List<User> getUser() {
            List<User> list = new ArrayList<User>();
            for (int i = 0; i < 3; i++) {
                User user = new User(i, "name" + i, i);
                list.add(user);
            }
            return list;
        }
    
    }

    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="dubbo_provider" />
    
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.luo.DemoService" ref="demoService" />
        <!-- 使用注解方式暴露接口 -->
        <!-- <dubbo:annotation package="com.luo.DemoService" /> -->
    </beans>

    springmvc.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd   
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-4.0.xsd"
        default-autowire="byName">
    
        <aop:aspectj-autoproxy />
        <context:component-scan base-package="com.luo" />
        <import resource="classpath:dubbo-provider.xml" />
    </beans>

    测试类Test:

    package com.test;
    
    import java.io.IOException;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
            context.start();
            
            System.out.println("Dubbo provider start...");
            
            try {
                System.in.read();    // 按任意键退出
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }
    }

    结构如图:

    6,项目consumer

    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="dubbo_consumer" />       
          <!-- 使用multicast广播注册中心暴露发现服务地址 -->    
        <dubbo:registry  protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />         
          <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->    
        <dubbo:reference id="demoService" interface="com.luo.DemoService" />    
    </beans>   

    springmvc.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
            http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd   
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-4.0.xsd"
        default-autowire="byName">
    
        <aop:aspectj-autoproxy />
        <context:component-scan base-package="com.luo" />
        <import resource="classpath:/dubbo-consumer.xml" />
    </beans>

    测试类Test:

    package com.test;
    
    import java.io.IOException;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.luo.DemoService;
    
    public class Test {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
    
            context.start();
            DemoService demoService = (DemoService) context.getBean("demoService");
            
            System.out.println(demoService.sayHello("哈哈哈"));
            System.out.println(demoService.getUser());
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }

    结构如图:

    7,下载zookeeper,然后进入bin,找到zkServer.cmd,双击运行。参考:https://blog.csdn.net/jingyangv587/article/details/78901508

    8,运行provider的Test类:

    9,运行consumer的Test类:

    二、admin平台管理

        参考:https://blog.csdn.net/jingyangv587/article/details/78904369

    提供者:

    消费者:

     三、搭建web项目。

          1、  搭建子项目test-web,create simple...不选,next,如图:

    选择webapp这个,然后创建。

    2、会报错,根据错误提示,搜索然后修复好。

    3,参照上面和 https://www.cnblogs.com/luoa/articles/9727008.html,把项目做成spring mvc,并把一些文件抄过来,其中,注意修改web.xml。

    HelloController:

    package com.aluo.controller;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.luo.DemoService;
    
    @Controller
    public class HelloController {
        @Resource(name="demoService")
        private DemoService demoService;
        
        @RequestMapping("/welcome")
        public String welcome(ModelMap model) {
            System.out.println("begin");
            System.out.println(demoService.getUser());
            model.addAttribute("message", demoService.sayHello("hello dubbo"));
            return "welcome";
        }
    
    }

    结构如图:

    4,运行zookeeper,和provider,再把项目加入tomcat运行,访问 http://localhost:8080/test-web/welcome,如图:

       

    整个项目结构图:

  • 相关阅读:
    利用子查询解决复杂sql问题
    如何用临时表代替游标进行表记录的拷贝
    SQL新函数, 排名函数 ROW_NUMBER(), RANK(), DENSE_RANK()
    SQL SERVER2005 中的错误捕捉与处理
    用户自定义函数代替游标进行循环拼接
    使用游标进行循环数据插入
    Oracle中利用存储过程建表
    SQL SERVER中强制类型转换cast和convert的区别
    SQL如何修改被计算字段引用的字段类型
    1.官方网站:http://www.mplayerhq.hu/design7/dload.html
  • 原文地址:https://www.cnblogs.com/luoa/p/10422218.html
Copyright © 2020-2023  润新知