1.创建父工程
pom文件如下(这里只添加几个最简单的依赖)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 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 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>wyb</groupId> 8 <artifactId>springbootDubbo</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>api</module> 13 <module>provider</module> 14 <module>consumer</module> 15 </modules> 16 17 <!-- Spring Boot 启动父依赖 --> 18 <parent> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-parent</artifactId> 21 <version>1.5.1.RELEASE</version> 22 </parent> 23 24 <properties> 25 <dubbo-spring-boot>1.0.0</dubbo-spring-boot> 26 </properties> 27 28 <dependencies> 29 30 <!-- Spring Boot Dubbo 依赖 --> 31 <dependency> 32 <groupId>io.dubbo.springboot</groupId> 33 <artifactId>spring-boot-starter-dubbo</artifactId> 34 <version>${dubbo-spring-boot}</version> 35 </dependency> 36 37 <!-- Spring Boot Web 依赖 --> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-web</artifactId> 41 </dependency> 42 43 <!-- Spring Boot Test 依赖 --> 44 <!--<dependency>--> 45 <!--<groupId>org.springframework.boot</groupId>--> 46 <!--<artifactId>spring-boot-starter-test</artifactId>--> 47 <!--<scope>test</scope>--> 48 <!--</dependency>--> 49 50 <!-- Junit --> 51 <!--<dependency>--> 52 <!--<groupId>junit</groupId>--> 53 <!--<artifactId>junit</artifactId>--> 54 <!--<version>4.12</version>--> 55 <!--</dependency>--> 56 </dependencies> 57 58 </project>
2.创建子工程api工程(主要是写一些实体和接口)
pom文件如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 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 <parent> 6 <artifactId>springbootDubbo</artifactId> 7 <groupId>wyb</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>api</artifactId> 13 <dependencies> 14 <dependency> 15 <groupId>org.hibernate.javax.persistence</groupId> 16 <artifactId>hibernate-jpa-2.1-api</artifactId> 17 <version>1.0.0.Final</version> 18 </dependency> 19 <dependency> 20 <groupId>org.hibernate.javax.persistence</groupId> 21 <artifactId>hibernate-jpa-2.1-api</artifactId> 22 <version>1.0.0.Final</version> 23 </dependency> 24 <dependency> 25 <groupId>org.apache.kafka</groupId> 26 <artifactId>kafka_2.11</artifactId> 27 <version>0.10.0.0</version> 28 <exclusions> 29 <exclusion> 30 <groupId>org.slf4j</groupId> 31 <artifactId>slf4j-log4j12</artifactId> 32 </exclusion> 33 </exclusions> 34 </dependency> 35 <dependency> 36 <groupId>com.oracle</groupId> 37 <artifactId>ojdbc6</artifactId> 38 <version>11.2.0.1.0</version> 39 </dependency> 40 <dependency> 41 <groupId>org.hibernate.javax.persistence</groupId> 42 <artifactId>hibernate-jpa-2.1-api</artifactId> 43 <version>1.0.0.Final</version> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework.data</groupId> 47 <artifactId>spring-data-jpa</artifactId> 48 <version>1.11.0.RELEASE</version> 49 </dependency> 50 </dependencies> 51 52 53 </project>
实体如下(Bszn)
1 package org.spring.springboot.domain; 2 3 import javax.persistence.*; 4 import java.io.Serializable; 5 6 @Entity 7 public class Bszn implements Serializable { 8 private final static long serialVersionUID = 0l; 9 @Id 10 @SequenceGenerator(name = "BSZN_ID_GENERATOR", sequenceName = "BSZN$SEQ", allocationSize = 1) 11 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BSZN_ID_GENERATOR") 12 private Long id; 13 14 @Column(name = "NAME") 15 private String name; 16 17 @Column(name = "VERSION") 18 private Long version; 19 20 public Long getId() { 21 return id; 22 } 23 24 public void setId(Long id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public Long getVersion() { 37 return version; 38 } 39 40 public void setVersion(Long version) { 41 this.version = version; 42 } 43 }
接口如下
1 package org.spring.springboot.dubbo; 2 3 import org.spring.springboot.domain.Bszn; 4 5 import java.util.List; 6 7 public interface BsznDubboService { 8 List<Bszn> getBszn(); 9 }
3.创建子工程provider工程(这里主要写接口的具体实现),因此在pom中必须添加api依赖
注:这里不需要再写实体
pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.jxust</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>wyb</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<!-- <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
resources文件配置如下:
1 spring: 2 dubbo: 3 application: 4 name: provider 5 registry: 6 address: multicast://224.5.6.7:1234(这里是不需要配置注册中心,直接使用组播协议,相当于本地使用) 7 protocol: 8 name: dubbo 9 port: 20880 10 scan: org.spring.springboot.dubbo 11 datasource: 12 driver-class-name: oracle.jdbc.OracleDriver 13 url: jdbc:oracle:thin:@//***/orcl 14 username: cs_test 15 password: quickdone 16 jpa: 17 hibernate: 18 ddl-auto: update 19 show-sql: true 20 server: 21 port: 8082
启动类如下
package org.spring.springboot; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List; @SpringBootApplication public class ServerApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { System.out.println("provider start..."); SpringApplication.run(ServerApplication.class, args); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // TODO Auto-generated method stub super.configureMessageConverters(converters); //1.需要先定义一个convert转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3.在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //4.将convert添加到converters中 converters.add(fastConverter); } }
dao层代码如下(这里只做简单的查询,所以继承了JpaRepository后无需再写方法)
1 package org.spring.springboot.dao; 2 3 import org.spring.springboot.domain.Bszn; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 6 public interface BsznRepository extends JpaRepository<Bszn, Integer> { 7 8 }
实现类如下(注解必须添加,需要和后面consumer做对应)
package org.spring.springboot.dubbo.impl; import com.alibaba.dubbo.config.annotation.Service; import org.spring.springboot.dao.BsznRepository; import org.spring.springboot.domain.Bszn; import org.spring.springboot.dubbo.BsznDubboService; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @Service(version = "1.0.0") public class BsznServiceImpl implements BsznDubboService{ @Autowired private BsznRepository bsznRepository; @Override public List<Bszn> getBszn() { List<Bszn> list = bsznRepository.findAll(); return list; } }
最后是创建子工程consumer工程
pom文件如下
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springbootDubbo</artifactId> <groupId>wyb</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consumer</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>wyb</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <resources> <!-- 确保webapp和resource资源能被访问 --> <resource> <directory>${basedir}/src/main/resources</directory> <includes> <include>**/**</include> </includes> </resource> </resources> <plugins> <!-- 直接运行项目的插件,我们可以直接mvn spring-boot:run运行 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
resources配置文件如下这里必须做到dubbo的注册地址和provider中的配置相同,不然会找不到)
server: port: 8081 spring: dubbo: application: name: consumer registry: address: multicast://224.5.6.7:1234 scan: org.spring.springboot.dubbo
启动类如下
package org.spring.springboot; import org.spring.springboot.dubbo.BsznDubboConsumerService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class ClientApplication { public static void main(String[] args) { // 程序启动入口 // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args); BsznDubboConsumerService bsznDubboConsumerService = run.getBean(BsznDubboConsumerService.class); bsznDubboConsumerService.printBszn(); } }
最后控制层如下
package org.spring.springboot.dubbo; import com.alibaba.dubbo.config.annotation.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spring.springboot.domain.Bszn; import org.springframework.stereotype.Component; import java.util.List; @Component public class BsznDubboConsumerService { private static final Logger logger = LoggerFactory.getLogger(BsznDubboConsumerService.class); @Reference(version = "1.0.0") BsznDubboService bsznDubboService; public void printBszn() { List<Bszn> 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案例就已搭建完成,这里只写了通过数据库进行查询。。。