http://blog.csdn.net/a67474506/article/details/61640548

Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌

SpringBoot整合Dubbo的话我们首先要先对Dubbo的启动这块了解一哈

dubbo源码分析:http://blog.csdn.net/flashflight/article/details/44318447

Dubbo会注册各种解析器,因为我们这边不会在使用XML配置了,所以主要关注的地方就是这块

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

通过BeanDefinitionParser解析,然后注册到Ioc容器中,而这里我们通过SpringBoot的自动配置,读取yml配置生成SpringBean

基本上我们只有用到 registry,providerprotocol ,application这些

然后暴漏服务和引用服务 通过annotationBean这个东东

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

因为使用了yml或者properties的方式来配置dubbo,所以我们还需要dubbo的AnnotionBean类,来扫描指定包下面的类.

这里集成dubbo的时候和前面集成其他东西的是差不多的,不过在使用了AnnotionBean类的时候,因为AnnotionBean类实现了BeanFactoryPostProcessor接口.

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

这里还有一篇资料:

http://blog.csdn.net/u011686226/article/details/53841227

刚开始不知道的时候日志里面提示是这样的

  1. @Bean method DubboAutoConfiguration.annotationBean is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface.
  2. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class.
  3. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.

解决了这个问题之后,下面的就可以直接按照原先的步骤来搞

dubbo-spring-boot-starter

Pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.ibigsea</groupId>
  5. <artifactId>dubbo-spring-boot-starter</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <boot.version>1.3.5.RELEASE</boot.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot</artifactId>
  15. <version>${boot.version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-autoconfigure</artifactId>
  20. <version>${boot.version}</version>
  21. </dependency>
  22. <!-- dubbo -->
  23. <dependency>
  24. <groupId>com.alibaba</groupId>
  25. <artifactId>dubbo</artifactId>
  26. <version>2.5.3</version>
  27. <exclusions>
  28. <exclusion>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring</artifactId>
  31. </exclusion>
  32. </exclusions>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.github.sgroschupf</groupId>
  36. <artifactId>zkclient</artifactId>
  37. <version>0.1</version>
  38. <exclusions>
  39. <exclusion>
  40. <artifactId>slf4j-api</artifactId>
  41. <groupId>org.slf4j</groupId>
  42. </exclusion>
  43. <exclusion>
  44. <artifactId>log4j</artifactId>
  45. <groupId>log4j</groupId>
  46. </exclusion>
  47. <exclusion>
  48. <artifactId>slf4j-log4j12</artifactId>
  49. <groupId>org.slf4j</groupId>
  50. </exclusion>
  51. </exclusions>
  52. </dependency>
  53. </dependencies>
  54. </project>

DubboProperties.Java

  1. package com.ibigsea.dubbo.autoconfigure;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import com.alibaba.dubbo.config.ApplicationConfig;
  4. import com.alibaba.dubbo.config.ProtocolConfig;
  5. import com.alibaba.dubbo.config.RegistryConfig;
  6. @ConfigurationProperties(prefix = DubboProperties.DUBBO_PREFIX)
  7. public class DubboProperties {
  8. public static final String DUBBO_PREFIX = "dubbo";
  9. private String scan;
  10. private ApplicationConfig application;
  11. private ProtocolConfig protocol;
  12. private RegistryConfig registry;
  13. public String getScan() {
  14. return scan;
  15. }
  16. public void setScan(String scan) {
  17. this.scan = scan;
  18. }
  19. public ApplicationConfig getApplication() {
  20. return application;
  21. }
  22. public void setApplication(ApplicationConfig application) {
  23. this.application = application;
  24. }
  25. public ProtocolConfig getProtocol() {
  26. return protocol;
  27. }
  28. public void setProtocol(ProtocolConfig protocol) {
  29. this.protocol = protocol;
  30. }
  31. public RegistryConfig getRegistry() {
  32. return registry;
  33. }
  34. public void setRegistry(RegistryConfig registry) {
  35. this.registry = registry;
  36. }
  37. }

DubboAutoConfiguration.java

  1. package com.ibigsea.dubbo.autoconfigure;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import com.alibaba.dubbo.config.ApplicationConfig;
  10. import com.alibaba.dubbo.config.ProtocolConfig;
  11. import com.alibaba.dubbo.config.RegistryConfig;
  12. import com.alibaba.dubbo.config.spring.AnnotationBean;
  13. @Configuration
  14. @EnableConfigurationProperties(DubboProperties.class)//开启属性注入,通过@autowired注入
  15. @ConditionalOnClass({AnnotationBean.class,ApplicationConfig.class,ProtocolConfig.class,RegistryConfig.class})
  16. public class DubboAutoConfiguration {
  17. @Autowired
  18. private DubboProperties prop;
  19. @Bean
  20. @ConditionalOnMissingBean(AnnotationBean.class)//容器中如果没有这个类,那么自动配置这个类
  21. public static AnnotationBean annotationBean(@Value("${dubbo.packageName}")String packageName) {
  22. AnnotationBean annotationBean = new AnnotationBean();
  23. annotationBean.setPackage(packageName);
  24. return annotationBean;
  25. }
  26. @Bean
  27. @ConditionalOnMissingBean(ApplicationConfig.class)//容器中如果没有这个类,那么自动配置这个类
  28. public ApplicationConfig applicationConfig() {
  29. ApplicationConfig applicationConfig = new ApplicationConfig();
  30. applicationConfig.setName(prop.getApplication().getName());
  31. return applicationConfig;
  32. }
  33. @Bean
  34. @ConditionalOnMissingBean(ProtocolConfig.class)//容器中如果没有这个类,那么自动配置这个类
  35. public ProtocolConfig protocolConfig() {
  36. ProtocolConfig protocolConfig = new ProtocolConfig();
  37. protocolConfig.setName(prop.getProtocol().getName());
  38. protocolConfig.setPort(prop.getProtocol().getPort());
  39. return protocolConfig;
  40. }
  41. @Bean
  42. @ConditionalOnMissingBean(RegistryConfig.class)//容器中如果没有这个类,那么自动配置这个类
  43. public RegistryConfig registryConfig() {
  44. RegistryConfig registryConfig = new RegistryConfig();
  45. registryConfig.setAddress(prop.getRegistry().getAddress());
  46. return registryConfig;
  47. }
  48. }

在resource目录下面的META-INF添加spring.factories文件

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.ibigsea.dubbo.autoconfigure.DubboAutoConfiguration

目录结构

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

服务提供者dubbo-provider

Pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.ibigsea</groupId>
  5. <artifactId>dubbo-provider</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <boot.version>1.3.7.RELEASE</boot.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.ibigsea</groupId>
  14. <artifactId>dubbo-spring-boot-starter</artifactId>
  15. <version>1.0-SNAPSHOT</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. <version>${boot.version}</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <version>${boot.version}</version>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>
  29. </project>

定义一个接口BaseService.java

为了贪图方便,我就不在重新弄一个jar包,然后服务提供者和服务消费都引用这个jar了

  1. package com.ibigsea.service;
  2. public interface BaseService {
  3. public String build(String str);
  4. }

HelloService.java

  1. package com.ibigsea.dubbo_provider.impl;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.ibigsea.service.BaseService;
  4. @Service(group="helloService", version="1.0")
  5. public class HelloService implements BaseService {
  6. @Override
  7. public String build(String str) {
  8. return "hello "+str+" !";
  9. }
  10. }

启动类APP

  1. package com.ibigsea;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * 是Spring Boot项目的核心注解,主要是开启自动配置
  7. */
  8. @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
  9. @RestController
  10. public class App {
  11. public static void main(String[] args) {
  12. SpringApplication.run(App.class, args);
  13. }
  14. }

application.yml配置

  1. dubbo :
  2. protocol :
  3. prot : -1
  4. name  : dubbo
  5. application :
  6. name : hello-world-app
  7. registry :
  8. address : zookeeper://127.0.0.1:2181
  9. packageName : com.ibigsea.dubbo_provider.impl
  10. server :
  11. port : 8083

目录结构

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

服务消费者dubbo-consume

Pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.ibigsea</groupId>
  5. <artifactId>dubbo-consume</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <boot.version>1.3.5.RELEASE</boot.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. <version>${boot.version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-test</artifactId>
  20. <version>${boot.version}</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!-- dubbo -->
  24. <dependency>
  25. <groupId>com.alibaba</groupId>
  26. <artifactId>dubbo</artifactId>
  27. <version>2.5.3</version>
  28. <scope>provided</scope>
  29. <exclusions>
  30. <exclusion>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring</artifactId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.ibigsea</groupId>
  38. <artifactId>dubbo-spring-boot-starter</artifactId>
  39. <version>1.0-SNAPSHOT</version>
  40. </dependency>
  41. </dependencies>
  42. </project>

同样的一个接口BaseService

  1. package com.ibigsea.service;
  2. public interface BaseService {
  3. public String build(String str);
  4. }

RefService.java

  1. package com.ibigsea.dubbo_consume.reference;
  2. import org.springframework.stereotype.Service;
  3. import com.alibaba.dubbo.config.annotation.Reference;
  4. import com.ibigsea.service.BaseService;
  5. @Service("refService")
  6. public class RefService {
  7. @Reference(group="helloService", version="1.0")
  8. private BaseService baseService;
  9. public String sayHello(String name){
  10. return baseService.build(name);
  11. }
  12. }

启动类APP

  1. package com.ibigsea;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.ibigsea.dubbo_consume.reference.RefService;
  8. /**
  9. * 是Spring Boot项目的核心注解,主要是开启自动配置
  10. */
  11. @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
  12. @RestController
  13. public class App {
  14. @Autowired
  15. private RefService refService;
  16. public static void main(String[] args) {
  17. SpringApplication.run(App.class, args);
  18. }
  19. @RequestMapping("/say")
  20. public String sayHello(String name) {
  21. return refService.sayHello(name);
  22. }
  23. }

Application.yml

  1. dubbo :
  2. protocol:
  3. prot : -1
  4. name  : dubbo
  5. application:
  6. name : hello-world-app
  7. registry:
  8. address : zookeeper://127.0.0.1:2181
  9. packageName : com.ibigsea.dubbo_consume.reference
  10. server :
  11. port : 8085

项目结构

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

分别启动dubbo-provider 和 dubbo-consume

我们可以再zookeeper的client里面看到相关信息

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

这个是服务提供者的信息

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

这个是服务消费者的信息

这个是访问结果

【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)-LMLPHP

 
 
04-01 03:36