SpringBoot與Dubbo整合下篇

  (1)pom.xml引入相關依賴jar包,以下:git

        <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)編寫服務的接口以及實現類,以下:github

package springboot.dao;

public interface DubboService {

    public String sayHello();
}

  接口實現類:web

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配置文件,內容以下:spring

<?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"),以下:
apache

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目錄下,啓動控制檯,輸入:tomcat

  

  至此,dubbo服務方提供已經開發完畢!springboot

  (6)接下來開發服務消費方,將上述jar包導入到項目中。若是是maven項目,還須要將jar繼續打包,將上述jar包修改爲DubboService-1.0.jar,輸入以下命令:app

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

  結束後,會在本地倉庫生成以下文件夾:maven

  

 (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

  繼續努力!

相關文章
相關標籤/搜索