Dubbo:整合 Spark Web 框架


#Dubbo


Spark Web 框架网址: http://sparkjava.com/

我们基于 Dubbo:第一个 Dubbo 项目 中的示例项目,集成 Spark Web。

项目结构

.
├── build.gradle
├── settings.gradle
├── contract
│   ├── src
│       └── main
│           └── java
│               └── demo
│                   └── contract
│                       └── DemoService.java
├── provider
│   ├── src
│   │   └── main
│           ├── java
│           │   └── demo
│           │       └── provider
│           │           ├── DemoServiceImpl.java
│           │           └── ProviderMain.java
│           └── resources
│               ├── dubbo-provider.xml
│               └── log4j.properties
└── web
    └── src
        └── main
            ├── java
            │   └── demo
            │       └── web
            │           └── WebApplication.java
            └── resources
                ├── dubbo-consumer.xml
                └── log4j.properties

build.gradle 内容:

group 'com.example'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

ext {
  dubboVersion = '2.7.1'
  zookeeperVersion = '3.4.10'
  junitVersion = '4.12'
}

allprojects {
  apply plugin: 'java'

  repositories {
    maven { url 'https://maven.aliyun.com/repository/public/' }
    mavenCentral()
  }

}

// 配置子项目 contract
project(":contract") {

  dependencies {

    testCompile "junit:junit:$junitVersion"

  }
}


project(":provider") {  // 配置子项目 provider

  dependencies {

    compile project(":contract") // 依赖子项目 contract
    compile "org.apache.dubbo:dubbo:$dubboVersion"
    compile "org.apache.dubbo:dubbo-dependencies-zookeeper:$dubboVersion"

    testCompile "junit:junit:$junitVersion"
  }

}


// 配置子项目 web
project(":web") {

  dependencies {
    compile project(":contract") // 依赖子项目 contract

    compile "com.sparkjava:spark-core:2.7.2" // 引入 spark web 框架

    compile "org.apache.dubbo:dubbo:$dubboVersion"
    compile "org.apache.dubbo:dubbo-dependencies-zookeeper:$dubboVersion"

    testCompile "junit:junit:$junitVersion"
  }

}

log4j.properties:

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %X{IP} - %m%n

contract 模块

DemoService 内容如下:

package demo.contract;

public interface DemoService {

    String sayHello(String name);

}

provider 模块

DemoServiceImpl 内容如下:

package demo.provider;

import demo.contract.DemoService;
import org.springframework.stereotype.Service;

@Service
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

ProviderMain 内容如下:

package demo.provider;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

@Configuration
@ComponentScan
@ImportResource({"classpath:dubbo-provider.xml"})
public class ProviderMain {
    public static void main(String[] args) throws IOException {
        new AnnotationConfigApplicationContext(ProviderMain.class);
        System.in.read();
    }
}

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="hello-world-app"></dubbo:application>

    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:service interface="demo.contract.DemoService"
                   class="demo.provider.DemoServiceImpl"/>

</beans>

web 模块

WebApplication 类内容如下:

package demo.web;

import demo.contract.DemoService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spark.Request;
import spark.Response;
import spark.Route;

import java.io.IOException;

import static spark.Spark.get;

@Configuration
@ComponentScan
@ImportResource({"classpath:dubbo-consumer.xml"})
public class WebApplication implements InitializingBean {

    @Autowired
    private DemoService demoService;

    @Override
    public void afterPropertiesSet() {

        // 默认监听端口 4567

        get("/hello", (request, response) -> demoService.sayHello("张三"));

    }

    public static void main(String[] args) {

        new AnnotationConfigApplicationContext(WebApplication.class);

    }

}

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="consumer-of-helloworld-app"/>

    <dubbo:consumer timeout="5000" />
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

    <dubbo:reference id="demoService" interface="demo.contract.DemoService" check="false"/>

</beans>

测试

启动 zookeeper:

$ zkServer.sh start

运行 provider 模块中的 ProviderMain 类,然后运行 web 模块中的 WebApplication 类。

打开浏览器访问 http://127.0.0.1:4567/hello,会看到Hello, 张三



( 本文完 )