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!

     

參考文檔

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息