zoukankan      html  css  js  c++  java
  • SpringBoot与Dubbo整合下篇

      (1)pom.xml引入相关依赖jar包,如下:

            <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>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.5</version>
                <exclusions>
                    <exclusion>                
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                    <exclusion>                
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>

      (2)编写服务的接口以及实现类,如下:

    package springboot.dao;
    
    public interface DubboService {
    
        public String sayHello();
    }

      接口实现类:

    package springboot.dao.imp;
    import org.springframework.stereotype.Service;
    import springboot.dao.DubboService;
    
    @Service("DubboService")
    public class DubboServiceImp implements DubboService{
    
        @Override
        public String sayHello() {
            
            return "Hello Dubbo";
        }
    
    }

      (3)在resources文件下建立服务方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="provider"/>
        <!-- 使用zookeeper做为注册中心 -->
        <dubbo:registry  protocol="zookeeper" address="zookeeper://172.31.19.224:2181"/>
        <!-- ref中的值要跟服务实现类中的@Server的值一致 -->
        <dubbo:service interface="springboot.dao.DubboService" ref="DubboService"></dubbo:service>
    </beans>

      (4)修改springboot启动类,启动时引入provider.xml文件,使用@ImportResource("classpath:provider.xml"),如下:

    package springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling
    @EnableCaching
    @ImportResource("classpath:consumer.xml")
    public class SpringbootApplication extends SpringBootServletInitializer{
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootApplication.class, args);
        }
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringbootApplication.class);
        }
    }

      至此,服务方已经配置完毕了,我这里将代码发布到tomcat上。如果想在本地测试话,需要更改一下启动端口,因为后面消费方也需要启动。

      (5)接下来我们将服务接口.class文件(即上述的DubboService.class接口)打成jar包,(服务消费方在项目中需要导入该jar包才能调用服务),进入到DubboService.class目录下,启动控制台,输入:

      

      至此,dubbo服务方提供已经开发完毕!

      (6)接下来开发服务消费方,将上述jar包导入到项目中。如果是maven项目,还需要将jar继续打包,将上述jar包修改成DubboService-1.0.jar,输入如下命令:

    mvn install:install-file -Dfile=DubboService-1.0.jar -DgroupId=dubboService -DartifactId=dubboService -Dversion=1.0 -Dpackaging=jar 

      结束后,会在本地仓库生成如下文件夹:

      

     (7)在resources文件下新建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="consumer" />
        <!-- zookeeper作为注册中心 -->
        <dubbo:registry  protocol="zookeeper" address="zookeeper://172.31.19.224:2181" />
        <!-- 生成远程服务代理 -->
        <dubbo:reference interface="springboot.dao.DubboService" id="dubboService"></dubbo:reference>
    </beans>

      (8)在pom.xml中引入刚刚打包生成的jar包,如下:

     <dependency>
                <groupId>dubboService</groupId>
                <artifactId>dubboService</artifactId>
                <version>1.0</version>
      </dependency>

      (9)新建Controller,如下:

    package springboot.web;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import springboot.dao.DubboService;
    
    
    @RestController
    public class HelloController {
        
        @Autowired
        private DubboService dubboService;
    
        @RequestMapping("/hello")
        public String hello(){
            return dubboService.sayHello();
        }
    }

      (10)修改启动类,如下:

    package springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling
    @EnableCaching
    @ImportResource("classpath:consumer.xml")
    public class SpringbootApplication extends SpringBootServletInitializer{
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootApplication.class, args);
        }
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringbootApplication.class);
        }
    }

      (11)测试验证,浏览器输入:http://localhost:8080/hello,显示如下:

        

      至此,springBoot与Dubbo整合完毕!过程中也遇到一些小问题,记录一下吧。

      (1)过程provider.xml,consumer.xml文件会报错,但是不影响项目运行,如下:Description Resource Path Location Type cvc-complex-type.2.4.c: The matching..........

               解决方法如下:http://blog.csdn.net/u010457406/article/details/50696390

      (2)项目没有报错,但是项目名出现了个红叉,百度搜了好多都不对,只有这个有效,地址如下:

          解决方法如下:http://blog.csdn.net/hongchangfirst/article/details/7663287

      继续努力!

  • 相关阅读:
    VIM
    函数指针
    《BOOST程序库完全开发指南》 第13章 编程语言支持
    《BOOST程序库完全开发指南》 第01章 Boost程序库总论
    gtest
    《BOOST程序库完全开发指南》 第04章 实用工具
    distcc配置
    《BOOST程序库完全开发指南》 第08章 算法
    Makefile
    《BOOST程序库完全开发指南》 第03章 内存管理
  • 原文地址:https://www.cnblogs.com/gdpuzxs/p/7235272.html
Copyright © 2011-2022 走看看