1.创建父工程

pom文件如下(这里只添加几个最简单的依赖)
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 4.0.0
6
7 wyb
8 springbootDubbo
9 pom
10 1.0-SNAPSHOT
11
12 api
13 provider
14 consumer
15
16
17
18
19 org.springframework.boot
20 spring-boot-starter-parent
21 1.5.1.RELEASE
22
23
24
25 1.0.0
26
27
28
29
30
31
32 io.dubbo.springboot
33 spring-boot-starter-dubbo
34 ${dubbo-spring-boot}
35
36
37
38
39 org.springframework.boot
40 spring-boot-starter-web
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2.创建子工程api工程(主要是写一些实体和接口)
pom文件如下
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
6 springbootDubbo
7 wyb
8 1.0-SNAPSHOT
9
10 4.0.0
11
12 api
13
14
15 org.hibernate.javax.persistence
16 hibernate-jpa-2.1-api
17 1.0.0.Final
18
19
20 org.hibernate.javax.persistence
21 hibernate-jpa-2.1-api
22 1.0.0.Final
23
24
25 org.apache.kafka
26 kafka_2.11
27 0.10.0.0
28
29
30 org.slf4j
31 slf4j-log4j12
32
33
34
35
36 com.oracle
37 ojdbc6
38 11.2.0.1.0
39
40
41 org.hibernate.javax.persistence
42 hibernate-jpa-2.1-api
43 1.0.0.Final
44
45
46 org.springframework.data
47 spring-data-jpa
48 1.11.0.RELEASE
49
50
51
52
53
实体如下(Bszn)
1 packageorg.spring.springboot.domain;2
3 import javax.persistence.*;4 importjava.io.Serializable;5
6 @Entity7 public class Bszn implementsSerializable {8 private final static long serialVersionUID = 0l;9 @Id10 @SequenceGenerator(name = "BSZN_ID_GENERATOR", sequenceName = "BSZN$SEQ", allocationSize = 1)11 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BSZN_ID_GENERATOR")12 privateLong id;13
14 @Column(name = "NAME")15 privateString name;16
17 @Column(name = "VERSION")18 privateLong version;19
20 publicLong getId() {21 returnid;22 }23
24 public voidsetId(Long id) {25 this.id =id;26 }27
28 publicString getName() {29 returnname;30 }31
32 public voidsetName(String name) {33 this.name =name;34 }35
36 publicLong getVersion() {37 returnversion;38 }39
40 public voidsetVersion(Long version) {41 this.version =version;42 }43 }
接口如下
1 packageorg.spring.springboot.dubbo;2
3 importorg.spring.springboot.domain.Bszn;4
5 importjava.util.List;6
7 public interfaceBsznDubboService {8 ListgetBszn();9 }
3.创建子工程provider工程(这里主要写接口的具体实现),因此在pom中必须添加api依赖
注:这里不需要再写实体
pom文件如下:
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">
4.0.0
com.jxust
provider
0.0.1-SNAPSHOT
jar
provider
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.4.2.RELEASE
UTF-8
UTF-8
wyb
api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-rest
com.alibaba
fastjson
1.2.15
com.oracle
ojdbc6
11.2.0.1.0
org.springframework.boot
spring-boot-starter-security
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
org.springframework.boot
spring-boot-maven-plugin
resources文件配置如下:
1 spring:2 dubbo:3 application:4 name: provider5 registry:6 address: multicast://224.5.6.7:1234(这里是不需要配置注册中心,直接使用组播协议,相当于本地使用)
7 protocol:8 name: dubbo9 port: 20880
10 scan: org.spring.springboot.dubbo11 datasource:12 driver-class-name: oracle.jdbc.OracleDriver13 url: jdbc:oracle:thin:@/orcl
14 username: cs_test15 password: quickdone16 jpa:17 hibernate:18 ddl-auto: update19 show-sql: true
20 server:21 port: 8082
启动类如下
packageorg.spring.springboot;importcom.alibaba.fastjson.serializer.SerializerFeature;importcom.alibaba.fastjson.support.config.FastJsonConfig;importcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.http.converter.HttpMessageConverter;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;importjava.util.List;
@SpringBootApplicationpublic class ServerApplication extendsWebMvcConfigurerAdapter {public static voidmain(String[] args) {
System.out.println("provider start...");
SpringApplication.run(ServerApplication.class, args);
}
@Overridepublic void configureMessageConverters(List>converters) {//TODO Auto-generated method stub
super.configureMessageConverters(converters);//1.需要先定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = newFastJsonHttpMessageConverter();//2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = newFastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);//4.将convert添加到converters中
converters.add(fastConverter);
}
}
dao层代码如下(这里只做简单的查询,所以继承了JpaRepository后无需再写方法)
1 packageorg.spring.springboot.dao;2
3 importorg.spring.springboot.domain.Bszn;4 importorg.springframework.data.jpa.repository.JpaRepository;5
6 public interface BsznRepository extends JpaRepository{7
8 }
实现类如下(注解必须添加,需要和后面consumer做对应)
packageorg.spring.springboot.dubbo.impl;importcom.alibaba.dubbo.config.annotation.Service;importorg.spring.springboot.dao.BsznRepository;importorg.spring.springboot.domain.Bszn;importorg.spring.springboot.dubbo.BsznDubboService;importorg.springframework.beans.factory.annotation.Autowired;importjava.util.List;
@Service(version= "1.0.0")public class BsznServiceImpl implementsBsznDubboService{
@AutowiredprivateBsznRepository bsznRepository;
@Overridepublic ListgetBszn() {
List list =bsznRepository.findAll();returnlist;
}
}
最后是创建子工程consumer工程
pom文件如下
springbootDubbo
wyb
1.0-SNAPSHOT
4.0.0
consumer
jar
wyb
api
1.0-SNAPSHOT
${basedir}/src/main/resources
**/**
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
1.7
resources配置文件如下这里必须做到dubbo的注册地址和provider中的配置相同,不然会找不到)
server:
port:8081spring:
dubbo:
application:
name: consumer
registry:
address: multicast://224.5.6.7:1234
scan: org.spring.springboot.dubbo
启动类如下
packageorg.spring.springboot;importorg.spring.springboot.dubbo.BsznDubboConsumerService;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.ConfigurableApplicationContext;
@SpringBootApplicationpublic classClientApplication {public static voidmain(String[] args) {//程序启动入口//启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
BsznDubboConsumerService bsznDubboConsumerService= run.getBean(BsznDubboConsumerService.class);
bsznDubboConsumerService.printBszn();
}
}
最后控制层如下
packageorg.spring.springboot.dubbo;importcom.alibaba.dubbo.config.annotation.Reference;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.spring.springboot.domain.Bszn;importorg.springframework.stereotype.Component;importjava.util.List;
@Componentpublic classBsznDubboConsumerService {private static final Logger logger = LoggerFactory.getLogger(BsznDubboConsumerService.class);
@Reference(version= "1.0.0")
BsznDubboService bsznDubboService;public voidprintBszn() {
List list =bsznDubboService.getBszn();for (int i = 0; i < list.size(); i++) {if (i < 10) {
System.out.println("id=" + list.get(i).getId() + ",name=" + list.get(i).getName() + ",version=" +list.get(i).getVersion());
}else{break;
}
}
}
}
至此,简单的dubbo案例就已搭建完成,这里只写了通过数据库进行查询。。。