【转】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,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>
同样的一个接口BaseService
- 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配置)的更多相关文章
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]
https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...
- springboot学习笔记-5 springboot整合shiro
shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- SpringBoot学习笔记(16)----SpringBoot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(15)----SpringBoot使用Druid
直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存
1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...
随机推荐
- MongoDB 线上环境按照及配置(授权方式启动)
1创建文件repo文件 #vim /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Repository bas ...
- jsp servlet基础复习 Part1
jsp和servlet的一些基础知识整理,用于备忘. 一.jsp与servlet的基本关系 1.jsp-->web容器-->servlet-->加载进容器的虚拟机执行-->输出 ...
- cygwin 的安装和配置
Cygwin是一个在windows平台上运行的类UNIX模拟环境,是cygnus solutions公司开发的自由软件(该公司开发的著名工具还有eCos,不过现已被Redhat收购).它对于 ...
- Nginx 502错误总结
http请求流程:一般情况下,提交动态请求的时候,nginx会直接把 请求转交给php-fpm,而php-fpm再分配php-cgi进程来处理相关的请求,之后再依次返回,最后由nginx把结果反馈给客 ...
- RabbitMQ基础--总结
一. RabbitMQ的五种工作场景: 1. 单发单收 2. 单发送多接收 +++++++++++++++++++++前面两种没有使用exchange++++++++++++++++++ 3. Pub ...
- vscode 快速生成html
在Hbuilder中新建一个htm自动会生成一个标准的html代码,那在vscode得一行一行写吗?太烦了吧,各种关键词搜,哎妈 终于找到了办法,现在这里记录下: 第一步:在空文档中输入 ! 第二 ...
- Spring 框架(二)
1AOP 1.1 AOP介绍 1.1.1 什么是AOP l 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...
- CentOS 7运维管理笔记(12)----GUI配置工具Webmin的安装
早期的Linux系统管理员或是Web管理员在修改服务器配置时使用最多的就是vi编辑器,但是现在越来越多的基于GUI界面的配置工具出现了,毕竟人们还是喜欢以直接的可视化的方式来修改服务器的配置,而不是再 ...
- chengfa
public class ddddd{ public static void main(String[] args) { ; ; i <= m; i++) { ; j <= i; j++) ...
- Spring @Value SpEl 知识点小记
在JavaBean文件中使用Spring的@Value注解获取配置文件.yml或资源文件.properties中 key - value 键值信息 @Value("${stu.number} ...