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

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

 

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

 

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

 

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

 

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

 

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

 

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

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


 

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

 

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


 

这里还有一篇资料:

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

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

[html]  view plain  copy
 
  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

[html]  view plain  copy
 
  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.       
  8.     <properties>  
  9.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.         <boot.version>1.3.5.RELEASE</boot.version>  
  11.     </properties>  
  12.       
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework.boot</groupId>  
  16.             <artifactId>spring-boot</artifactId>  
  17.             <version>${boot.version}</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-autoconfigure</artifactId>  
  22.             <version>${boot.version}</version>  
  23.         </dependency>  
  24.         <!-- dubbo -->  
  25.         <dependency>  
  26.             <groupId>com.alibaba</groupId>  
  27.             <artifactId>dubbo</artifactId>  
  28.             <version>2.5.3</version>  
  29.             <exclusions>  
  30.                 <exclusion>  
  31.                     <groupId>org.springframework</groupId>  
  32.                     <artifactId>spring</artifactId>  
  33.                 </exclusion>  
  34.             </exclusions>  
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>com.github.sgroschupf</groupId>  
  38.             <artifactId>zkclient</artifactId>  
  39.             <version>0.1</version>  
  40.             <exclusions>  
  41.                 <exclusion>  
  42.                     <artifactId>slf4j-api</artifactId>  
  43.                     <groupId>org.slf4j</groupId>  
  44.                 </exclusion>  
  45.                 <exclusion>  
  46.                     <artifactId>log4j</artifactId>  
  47.                     <groupId>log4j</groupId>  
  48.                 </exclusion>  
  49.                 <exclusion>  
  50.                     <artifactId>slf4j-log4j12</artifactId>  
  51.                     <groupId>org.slf4j</groupId>  
  52.                 </exclusion>  
  53.             </exclusions>  
  54.         </dependency>  
  55.     </dependencies>  
  56. </project>  


 

DubboProperties.Java

[java]  view plain  copy
 
  1. package com.ibigsea.dubbo.autoconfigure;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4.   
  5. import com.alibaba.dubbo.config.ApplicationConfig;  
  6. import com.alibaba.dubbo.config.ProtocolConfig;  
  7. import com.alibaba.dubbo.config.RegistryConfig;  
  8.   
  9. @ConfigurationProperties(prefix = DubboProperties.DUBBO_PREFIX)  
  10. public class DubboProperties {  
  11.       
  12.     public static final String DUBBO_PREFIX = "dubbo";  
  13.       
  14.     private String scan;  
  15.       
  16.     private ApplicationConfig application;  
  17.       
  18.     private ProtocolConfig protocol;  
  19.       
  20.     private RegistryConfig registry;  
  21.       
  22.       
  23.     public String getScan() {  
  24.         return scan;  
  25.     }  
  26.   
  27.     public void setScan(String scan) {  
  28.         this.scan = scan;  
  29.     }  
  30.   
  31.     public ApplicationConfig getApplication() {  
  32.         return application;  
  33.     }  
  34.   
  35.     public void setApplication(ApplicationConfig application) {  
  36.         this.application = application;  
  37.     }  
  38.   
  39.     public ProtocolConfig getProtocol() {  
  40.         return protocol;  
  41.     }  
  42.   
  43.     public void setProtocol(ProtocolConfig protocol) {  
  44.         this.protocol = protocol;  
  45.     }  
  46.   
  47.     public RegistryConfig getRegistry() {  
  48.         return registry;  
  49.     }  
  50.   
  51.     public void setRegistry(RegistryConfig registry) {  
  52.         this.registry = registry;  
  53.     }  
  54.       
  55. }  


 

DubboAutoConfiguration.java

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


 

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


 

[html]  view plain  copy
 
  1. # Auto Configure  
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\  
  3. com.ibigsea.dubbo.autoconfigure.DubboAutoConfiguration  


 

 

目录结构


 

 

服务提供者dubbo-provider

 

Pom.xml

[html]  view plain  copy
 
  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.   
  8.     <properties>  
  9.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  10.         <boot.version>1.3.7.RELEASE</boot.version>  
  11.     </properties>  
  12.   
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>com.ibigsea</groupId>  
  16.             <artifactId>dubbo-spring-boot-starter</artifactId>  
  17.             <version>1.0-SNAPSHOT</version>  
  18.         </dependency>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-starter-web</artifactId>  
  22.             <version>${boot.version}</version>  
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-test</artifactId>  
  27.             <version>${boot.version}</version>  
  28.             <scope>test</scope>  
  29.         </dependency>  
  30.     </dependencies>  
  31.   
  32. </project>  


 

定义一个接口BaseService.java

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

[java]  view plain  copy
 
  1. package com.ibigsea.service;  
  2.   
  3. public interface BaseService {  
  4.       
  5.     public String build(String str);  
  6.       
  7. }  

HelloService.java

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


 

启动类APP

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


 

application.yml配置

[html]  view plain  copy
 
  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.   
  11. server :  
  12.   port : 8083  


 

目录结构


 

 

服务消费者dubbo-consume


 

 

Pom.xml

[html]  view plain  copy
 
  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.       
  8.       
  9.   
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.         <boot.version>1.3.5.RELEASE</boot.version>  
  13.     </properties>  
  14.   
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>org.springframework.boot</groupId>  
  18.             <artifactId>spring-boot-starter-web</artifactId>  
  19.             <version>${boot.version}</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.springframework.boot</groupId>  
  23.             <artifactId>spring-boot-starter-test</artifactId>  
  24.             <version>${boot.version}</version>  
  25.             <scope>test</scope>  
  26.         </dependency>  
  27.         <!-- dubbo -->  
  28.         <dependency>  
  29.             <groupId>com.alibaba</groupId>  
  30.             <artifactId>dubbo</artifactId>  
  31.             <version>2.5.3</version>  
  32.             <scope>provided</scope>  
  33.             <exclusions>  
  34.                 <exclusion>  
  35.                     <groupId>org.springframework</groupId>  
  36.                     <artifactId>spring</artifactId>  
  37.                 </exclusion>  
  38.             </exclusions>  
  39.         </dependency>  
  40.         <dependency>  
  41.             <groupId>com.ibigsea</groupId>  
  42.             <artifactId>dubbo-spring-boot-starter</artifactId>  
  43.             <version>1.0-SNAPSHOT</version>  
  44.         </dependency>  
  45.     </dependencies>  
  46.       
  47.   
  48. </project>  
同样的一个接口BaseService

 

[java]  view plain  copy
 
  1. package com.ibigsea.service;  
  2.   
  3. public interface BaseService {  
  4.       
  5.     public String build(String str);  
  6.       
  7. }  

 

RefService.java

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

 

启动类APP

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

 

Application.yml

[html]  view plain  copy
 
  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.     
  11. server :  
  12.   port : 8085  

项目结构

 

分别启动dubbo-provider 和 dubbo-consume

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

这个是服务提供者的信息

 

这个是服务消费者的信息

 

这个是访问结果

 
 

转载于:https://www.cnblogs.com/exmyth/p/7156568.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值