spring-boot-starter-dubbo

spring-boot-starter-dubbo的maven项目托管在https://github.com/xionghuiCoder/spring-boot-starter-dubbo;同时也能够在https://www.oschina.net/p/spring-boot-starter-dubbo上了解它的简单介绍。linux

spring-boot-starter-dubbo是dubbo的spring boot starter,它能够无缝地对接spring boot和dubbo,方便你们使用dubbo组件。git

须要注意的是spring-boot-starter-dubbo支持的jdk版本为1.6或者1.6+。github

如何发布dubbo服务

  • 添加spring-boot-starter-dubbo依赖:
  • <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>spring-boot-starter-dubbo</artifactId>
    	<version>1.0.0-SNAPSHOT</version>
    </dependency>

    若是注册中心使用zookeeper的话须要添加zkclient依赖:web

  • <dependency>
    	<groupId>com.101tec</groupId>
    	<artifactId>zkclient</artifactId>
    	<version>0.10</version>
    	<exclusions>
    		<exclusion>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    		</exclusion>
    	</exclusions>
    </dependency>

     

  • 在application.properties内添加dubbo的相关配置信息,demo配置以下:
  • # dubbo配置
    spring.dubbo.appname=provider-test
    spring.dubbo.protocol=dubbo
    # 此处也可以使用其它协议,好比zookeeper://xxxx:xx
    spring.dubbo.registry=multicast://224.0.0.0:1111
    spring.dubbo.port=20800
    spring.dubbo.group=test
    spring.dubbo.version=1.0.0

     

  • 接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。
  • /**
     * Provider启动类 <br />
     *
     * 若是没有web容器,须要hold住服务,不然进程会退出,参考如下代码:
     *
     * <pre>
     * synchronized (DubboProviderLauncher.class) {
     *   while (true) {
     *     try {
     *       DubboProviderLauncher.class.wait();
     *     } catch (InterruptedException e) {
     *       // swallow it
     *     }
     *   }
     * }
     * </pre>
     *
     * @author xionghui
     * @email xionghui.xh@alibaba-inc.com
     * @since 1.0.0
     */
    @SpringBootApplication
    @EnableDubboConfiguration
    public class DubboProviderLauncher {
    
      public static void main(String[] args) {
        SpringApplication.run(DubboProviderLauncher.class, args);
      }
    }

     

  • 编写hello服务,只须要在发布的服务实现上添加@Service注解 ,其中interfaceClass是要发布服务的接口。
  • import com.alibaba.dubbo.config.annotation.Service;
    
    @Component
    @Service(interfaceClass = IHelloService.class)
    public class HelloServiceImpl implements IHelloService {
    
      @Override
      public String hello() {
        return "hi, you!";
      }
    
    }

     

  • 启动Spring Boot应用。
  • 若是provider使用了web容器,能够使用http://localhost:port/dubbo/offline来下线全部服务;该接口会返回一个数组,数组里面是下线服务的详细信息:
  • [
    	{
    		side: "provider",
    		methods: "hello",
    		dubbo: "2.5.3",
    		threads: "200",
    		pid: "19919",
    		interface: "org.test.IHelloService",
    		version: "1.0.0",
    		revision: "1.0.0",
    		path: "org.test.IHelloService",
    		protocol: "dubbo",
    		application: "provider-test",
    		port: "20800",
    		host: "192.168.0.10",
    		anyhost: "true",
    		group: "test",
    		timestamp: "1484403167472"
    	}
    ]

    还能够使用http://localhost:port/dubbo/online来上线全部服务;该接口会返回一个数组,数组里面是上线服务的详细信息:spring

  • [
    	{
    		side: "provider",
    		methods: "hello",
    		dubbo: "2.5.3",
    		threads: "200",
    		pid: "19919",
    		interface: "org.test.IHelloService",
    		version: "1.0.0",
    		revision: "1.0.0",
    		path: "org.test.IHelloService",
    		protocol: "dubbo",
    		application: "provider-test",
    		port: "20800",
    		host: "192.168.0.10",
    		anyhost: "true",
    		group: "test",
    		timestamp: "1484403167472"
    	}
    ]

     

如何消费Dubbo服务

  • 添加依赖:
  • <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>spring-boot-starter-dubbo</artifactId>
    	<version>1.0.0-SNAPSHOT</version>
    </dependency>
  • 一样,若是注册中心使用zookeeper的话须要添加zkclient依赖:
  • <dependency>
    	<groupId>com.101tec</groupId>
    	<artifactId>zkclient</artifactId>
    	<version>0.10</version>
    	<exclusions>
    		<exclusion>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    		</exclusion>
    	</exclusions>
    </dependency>

     

  • 在application.properties添加dubbo的相关配置信息,demo配置以下:
  • # dubbo配置
    spring.dubbo.appname=consumer-test
    spring.dubbo.protocol=dubbo
    # 此处也可以使用其它协议,好比zookeeper://xxxx:xx
    spring.dubbo.registry=multicast://224.0.0.0:1111
    spring.dubbo.port=20801
    spring.dubbo.group=test
    spring.dubbo.version=1.0.0

     

  • 接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。
  • /**
     * Consumer启动类
     *
     * @author xionghui
     * @email xionghui.xh@alibaba-inc.com
     * @since 1.0.0
     */
    @SpringBootApplication
    @EnableDubboConfiguration
    public class DubboConsumerLauncher {
    
      public static void main(String[] args) {
        SpringApplication.run(DubboConsumerLauncher.class, args);
      }
    }

     

  • 经过@DubboConsumer注入须要使用的interface:
  • /**
     * Consumer Controller
     *
     * @author xionghui
     * @email xionghui.xh@alibaba-inc.com
     * @since 1.0.0
     */
    @RestController
    @RequestMapping("/")
    public class ConsumerController {
      // timeout表示dubbo超时时间,单位为ms
      @DubboConsumer(timeout = 1000)
      private IHelloService iHelloService;
    
      @RequestMapping(value = "hello", method = RequestMethod.GET)
      @ResponseBody
      public String hello() {
        return this.iHelloService.hello();
      }
    }

     

  • 启动Spring Boot应用。
  • 能够经过http://localhost:port/health监控服务信息:
  • {
    	status: "UP",
    	dubbo: {
    		status: "UP",
    		ClassIdBean [clazz=interface org.test.IHelloService, group=test, version=1.0.0]: true
    	},
    	diskSpace: {
    		status: "UP",
    		total: 487955914752,
    		free: 455584600064,
    		threshold: 10485760
    	}
    }

     

  • 最后经过http://localhost:port/hello调用RPC服务,返回结果:
  • hi, you!

     

参考文档

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息