SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌
SpringBoot整合Dubbo的话我们首先要先对Dubbo的启动这块了解一哈
dubbo源码分析:http://blog.csdn.net/flashflight/article/details/44318447
Dubbo会注册各种解析器,因为我们这边不会在使用XML配置了,所以主要关注的地方就是这块
通过BeanDefinitionParser解析,然后注册到Ioc容器中,而这里我们通过SpringBoot的自动配置,读取yml配置生成SpringBean
基本上我们只有用到 registry,provider, protocol ,application这些
然后暴漏服务和引用服务 通过annotationBean这个东东
因为使用了yml或者properties的方式来配置dubbo,所以我们还需要dubbo的AnnotionBean类,来扫描指定包下面的类.
这里集成dubbo的时候和前面集成其他东西的是差不多的,不过在使用了AnnotionBean类的时候,因为AnnotionBean类实现了BeanFactoryPostProcessor接口.
这里还有一篇资料:
http://blog.csdn.net/u011686226/article/details/53841227
刚开始不知道的时候日志里面提示是这样的
- @Bean method DubboAutoConfiguration.annotationBean is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface.
- This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class.
- 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
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.5.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-api</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- <exclusion>
- <artifactId>log4j</artifactId>
- <groupId>log4j</groupId>
- </exclusion>
- <exclusion>
- <artifactId>slf4j-log4j12</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- </project>
DubboProperties.java
- package com.ibigsea.dubbo.autoconfigure;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import com.alibaba.dubbo.config.ApplicationConfig;
- import com.alibaba.dubbo.config.ProtocolConfig;
- import com.alibaba.dubbo.config.RegistryConfig;
- @ConfigurationProperties(prefix = DubboProperties.DUBBO_PREFIX)
- public class DubboProperties {
- public static final String DUBBO_PREFIX = "dubbo";
- private String scan;
- private ApplicationConfig application;
- private ProtocolConfig protocol;
- private RegistryConfig registry;
- public String getScan() {
- return scan;
- }
- public void setScan(String scan) {
- this.scan = scan;
- }
- public ApplicationConfig getApplication() {
- return application;
- }
- public void setApplication(ApplicationConfig application) {
- this.application = application;
- }
- public ProtocolConfig getProtocol() {
- return protocol;
- }
- public void setProtocol(ProtocolConfig protocol) {
- this.protocol = protocol;
- }
- public RegistryConfig getRegistry() {
- return registry;
- }
- public void setRegistry(RegistryConfig registry) {
- this.registry = registry;
- }
- }
DubboAutoConfiguration.java
- package com.ibigsea.dubbo.autoconfigure;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- 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;
- @Configuration
- @EnableConfigurationProperties(DubboProperties.class)//开启属性注入,通过@autowired注入
- @ConditionalOnClass({AnnotationBean.class,ApplicationConfig.class,ProtocolConfig.class,RegistryConfig.class})
- public class DubboAutoConfiguration {
- @Autowired
- private DubboProperties prop;
- @Bean
- @ConditionalOnMissingBean(AnnotationBean.class)//容器中如果没有这个类,那么自动配置这个类
- public static AnnotationBean annotationBean(@Value("${dubbo.packageName}")String packageName) {
- AnnotationBean annotationBean = new AnnotationBean();
- annotationBean.setPackage(packageName);
- return annotationBean;
- }
- @Bean
- @ConditionalOnMissingBean(ApplicationConfig.class)//容器中如果没有这个类,那么自动配置这个类
- public ApplicationConfig applicationConfig() {
- ApplicationConfig applicationConfig = new ApplicationConfig();
- applicationConfig.setName(prop.getApplication().getName());
- return applicationConfig;
- }
- @Bean
- @ConditionalOnMissingBean(ProtocolConfig.class)//容器中如果没有这个类,那么自动配置这个类
- public ProtocolConfig protocolConfig() {
- ProtocolConfig protocolConfig = new ProtocolConfig();
- protocolConfig.setName(prop.getProtocol().getName());
- protocolConfig.setPort(prop.getProtocol().getPort());
- return protocolConfig;
- }
- @Bean
- @ConditionalOnMissingBean(RegistryConfig.class)//容器中如果没有这个类,那么自动配置这个类
- public RegistryConfig registryConfig() {
- RegistryConfig registryConfig = new RegistryConfig();
- registryConfig.setAddress(prop.getRegistry().getAddress());
- return registryConfig;
- }
- }
在resource目录下面的META-INF添加spring.factories文件
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.ibigsea.dubbo.autoconfigure.DubboAutoConfiguration
目录结构
服务提供者dubbo-provider
Pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>dubbo-provider</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.7.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.ibigsea</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <version>${boot.version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
定义一个接口BaseService.java
为了贪图方便,我就不在重新弄一个jar包,然后服务提供者和服务消费都引用这个jar了
- package com.ibigsea.service;
- public interface BaseService {
- public String build(String str);
- }
HelloService.java
- package com.ibigsea.dubbo_provider.impl;
- import com.alibaba.dubbo.config.annotation.Service;
- import com.ibigsea.service.BaseService;
- @Service(group="helloService", version="1.0")
- public class HelloService implements BaseService {
- @Override
- public String build(String str) {
- return "hello "+str+" !";
- }
- }
启动类APP
- package com.ibigsea;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * 是Spring Boot项目的核心注解,主要是开启自动配置
- */
- @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
- @RestController
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- }
application.yml配置
- dubbo :
- protocol :
- prot : -1
- name : dubbo
- application :
- name : hello-world-app
- registry :
- address : zookeeper://127.0.0.1:2181
- packageName : com.ibigsea.dubbo_provider.impl
- server :
- port : 8083
目录结构
服务消费者dubbo-consume
Pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>dubbo-consume</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.5.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <version>${boot.version}</version>
- <scope>test</scope>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- <scope>provided</scope>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.ibigsea</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
- </project>
- package com.ibigsea.service;
- public interface BaseService {
- public String build(String str);
- }
RefService.java
- package com.ibigsea.dubbo_consume.reference;
- import org.springframework.stereotype.Service;
- import com.alibaba.dubbo.config.annotation.Reference;
- import com.ibigsea.service.BaseService;
- @Service("refService")
- public class RefService {
- @Reference(group="helloService", version="1.0")
- private BaseService baseService;
- public String sayHello(String name){
- return baseService.build(name);
- }
- }
启动类APP
- package com.ibigsea;
- 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.RestController;
- import com.ibigsea.dubbo_consume.reference.RefService;
- /**
- * 是Spring Boot项目的核心注解,主要是开启自动配置
- */
- @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
- @RestController
- public class App {
- @Autowired
- private RefService refService;
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- @RequestMapping("/say")
- public String sayHello(String name) {
- return refService.sayHello(name);
- }
- }
Application.yml
- dubbo :
- protocol:
- prot : -1
- name : dubbo
- application:
- name : hello-world-app
- registry:
- address : zookeeper://127.0.0.1:2181
- packageName : com.ibigsea.dubbo_consume.reference
- server :
- port : 8085
项目结构
分别启动dubbo-provider 和 dubbo-consume
我们可以再zookeeper的client里面看到相关信息
这个是服务提供者的信息
这个是服务消费者的信息
这个是访问结果
相关推荐
- SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
- dubbo学习笔记(三)——整合SpringBoot
- springboot学习笔记(十二) SpringBoot项目中使用SpringSecurity整合OAuth2设计项目API安全接口服务
- SpringBoot学习笔记(八):SpringBoot启动端口+访问路、SpringBoot配置文件yml、SpringBoot多环境区分、SpringBoot打包发布
- SpringBoot学习笔记之application.yml配置文件
- SpringBoot2整合dubbo使用Zookeeper作为注册中心,使用yml配置
- springBoot学习笔记之application.yml配置文件
- SpringBoot2整合dubbo使用Zookeeper作为注册中心,使用yml配置
- SpringBoot2整合dubbo使用Zookeeper作为注册中心,使用yml配置
- 笔记-集成学习-stacking
- SpringBoot 结合mybatis-plus整理