1.建立父工程java
pom文件以下(這裏只添加幾個最簡單的依賴)mysql
<?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工程(主要是寫一些實體和接口)web
pom文件以下spring
<?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)sql
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依賴apache
注:這裏不須要再寫實體json
pom文件以下:api
<?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文件配置以下:springboot
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案例就已搭建完成,這裏只寫了經過數據庫進行查詢。。。