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

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

 

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

 

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

 

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

SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第1张图片

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

 

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

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

SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第2张图片

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

 

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


SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第3张图片

这里还有一篇资料:

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


	4.0.0
	com.ibigsea
	dubbo-spring-boot-starter
	1.0-SNAPSHOT
	
	
		UTF-8
		1.3.5.RELEASE
	
	
	
		
			org.springframework.boot
			spring-boot
			${boot.version}
		
		
			org.springframework.boot
			spring-boot-autoconfigure
			${boot.version}
		
		
		
			com.alibaba
			dubbo
			2.5.3
			
				
					org.springframework
					spring
				
			
		
		
			com.github.sgroschupf
			zkclient
			0.1
			
				
					slf4j-api
					org.slf4j
				
				
					log4j
					log4j
				
				
					slf4j-log4j12
					org.slf4j
				
			
		
	


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


目录结构

SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第4张图片


服务提供者dubbo-provider

Pom.xml


	4.0.0
	com.ibigsea
	dubbo-provider
	0.0.1-SNAPSHOT

	
		UTF-8
		1.3.7.RELEASE
	

	
		
			com.ibigsea
			dubbo-spring-boot-starter
			1.0-SNAPSHOT
		
		
			org.springframework.boot
			spring-boot-starter-web
			${boot.version}
		
		
			org.springframework.boot
			spring-boot-starter-test
			${boot.version}
			test
		
	



定义一个接口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


目录结构

SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第5张图片


服务消费者dubbo-consume


Pom.xml


	4.0.0
	com.ibigsea
	dubbo-consume
	0.0.1-SNAPSHOT
	
	

	
		UTF-8
		1.3.5.RELEASE
	

	
		
			org.springframework.boot
			spring-boot-starter-web
			${boot.version}
		
		
			org.springframework.boot
			spring-boot-starter-test
			${boot.version}
			test
		
		
		
			com.alibaba
			dubbo
			2.5.3
			provided
			
				
					org.springframework
					spring
				
			
		
		
			com.ibigsea
			dubbo-spring-boot-starter
			1.0-SNAPSHOT
		
	
	

同样的一个接口 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

项目结构

SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)_第6张图片

分别启动dubbo-provider dubbo-consume

我们可以再zookeeperclient里面看到相关信息

这个是服务提供者的信息


这个是服务消费者的信息

 

这个是访问结果


你可能感兴趣的:(spring,boot,dubbo,yml,spring,boot)