1.创建父工程
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>wyb</groupId>
<artifactId>springbootDubbo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>api</module>
<module>provider</module>
<module>consumer</module>
</modules>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-test</artifactId>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<!-- Junit -->
<!--<dependency>-->
<!--<groupId>junit</groupId>-->
<!--<artifactId>junit</artifactId>-->
<!--<version>4.12</version>-->
<!--</dependency>-->
</dependencies>
</project>
2.创建子工程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">
<parent>
<artifactId>springbootDubbo</artifactId>
<groupId>wyb</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.10.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.0.RELEASE</version>
</dependency>
</dependencies>
</project>
实体如下(Bszn)
package org.spring.springboot.domain;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Bszn implements Serializable {
private final static long serialVersionUID = 0l;
@Id
@SequenceGenerator(name = "BSZN_ID_GENERATOR", sequenceName = "BSZN$SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BSZN_ID_GENERATOR")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "VERSION")
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
接口如下
package org.spring.springboot.dubbo;
import org.spring.springboot.domain.Bszn;
import java.util.List;
public interface BsznDubboService {
List<Bszn> getBszn();
}
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文件配置如下:
spring:
dubbo:
application:
name: provider
registry:
address: multicast://224.5.6.7:1234(这里是不需要配置注册中心,直接使用组播协议,相当于本地使用)
protocol:
name: dubbo
port: 20880
scan: org.spring.springboot.dubbo
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@//***/orcl
username: cs_test
password: quickdone
jpa:
hibernate:
ddl-auto: update
show-sql: true
server:
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后无需再写方法)
package org.spring.springboot.dao;
import org.spring.springboot.domain.Bszn;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BsznRepository extends JpaRepository<Bszn, Integer> {
}
实现类如下(注解必须添加,需要和后面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案例就已搭建完成,这里只写了通过数据库进行查询。。。