spring-boot + dubbox 集成(无xml配置文件)

注:本教程环境为mac os

1:安装zookeeper

brew install zookeeper

启动:

zkServer start

2:安装dubbox

git clone https://github.com/dangdangdotcom/dubbox.git

mvn install -Dmaven.test.skip=true

3:IDEA建立spring-boot程序

new project -> Spring Initializr

其它按照ide步骤提示即可

4:pom.xml dependency

spring-boot + dubbox 集成(无xml配置文件)_第1张图片
pom.xml

4:配置dubbo

package com.example.config;

import com.alibaba.dubbo.config.ApplicationConfig;

import com.alibaba.dubbo.config.ProtocolConfig;

import com.alibaba.dubbo.config.RegistryConfig;

import com.alibaba.dubbo.config.spring.AnnotationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* Created by yhon on 2017/5/8.

*/

@Configuration

public class DubboConfig {

public static final String APPLICATION_NAME="provider-hello";

public static final String REGISTRY_ADDRESS="zookeeper://127.0.0.1:2181";

public static final String ANNOTATION_PACKAGE="com.example.provider";

@Bean

public ApplicationConfig applicationConfig() {

          ApplicationConfig applicationConfig =newApplicationConfig();

          applicationConfig.setName(APPLICATION_NAME);

          returnapplicationConfig;

}

@Bean

public RegistryConfig registryConfig() {

        RegistryConfig registryConfig =newRegistryConfig();

        registryConfig.setAddress(REGISTRY_ADDRESS);

        returnregistryConfig;

}

@Bean

public ProtocolConfig protocolConfig() {

        ProtocolConfig protocolConfig =newProtocolConfig();

        protocolConfig.setName("dubbo");

        protocolConfig.setPort(20880);

        returnprotocolConfig;

}

@Bean

public static AnnotationBean annotationBean() {

         AnnotationBean annotationBean =newAnnotationBean();

        annotationBean.setPackage(ANNOTATION_PACKAGE);

        returnannotationBean;

}

}

5:服务端

接口编写:

package com.example.provider;

/**

* Created by yhon on 2017/5/8.

*/

public interface HelloService {

       String sayHello();

}

service编写:

package com.example.provider;

import com.alibaba.dubbo.config.annotation.Service;

/**

* Created by yhon on 2017/5/8.

*/

@Service(version="0.0.1")

public class HelloServiceImp implements HelloService{

@Override

public String sayHello() {

       return"hello word";

}

}


6:消费端

消费端配置和服务端类似,只需修改ANNOTATION_PACKAGE = "com.example.service"

客户端调用代码:

package com.example.service;

import com.alibaba.dubbo.config.annotation.Reference;

import com.example.provider.HelloService;

import com.fasterxml.jackson.core.JsonProcessingException;

import org.springframework.stereotype.Service;

/**

* Created by yhon on 2017/5/9.

*/

@Service

public class HelloServiceImp {

@Reference(version="0.0.1")

private HelloService helloService;

public String sayHello(){

    return helloService.sayHello();

}

}

7:消费端编写接口测试

接口代码:

package com.example;

import com.example.service.HelloServiceImp;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import java.util.Map;

@SpringBootApplication

@RestController

public class DubboCustApplication {

@Autowired

private HelloServiceImp helloServiceImp;

@RequestMapping(value="/hello",method= RequestMethod.GET)

public Map getHello(){

String status =helloServiceImp.sayHello();

Map map =newHashMap<>();

map.put("message",status);

return map;

}

public static voidmain(String[] args) {

SpringApplication.run(DubboCustApplication.class,args);

}

}

至此浏览器中输入 127.0.0.1:8080/hello 即可得到如下字符串

{"status":"hello word"}

本人亲测可用,如有任何问题,可在评论区留言,谢谢。

你可能感兴趣的:(spring-boot + dubbox 集成(无xml配置文件))