摘要:創建父工程文件如下這里只添加幾個最簡單的依賴啟動父依賴依賴依賴依賴創建子工程
1.創建父工程
pom文件如下(這里只添加幾個最簡單的依賴)
4.0.0 wyb springbootDubbo pom 1.0-SNAPSHOT api provider consumer org.springframework.boot spring-boot-starter-parent 1.5.1.RELEASE 1.0.0 io.dubbo.springboot spring-boot-starter-dubbo ${dubbo-spring-boot} org.springframework.boot spring-boot-starter-web
2.創建子工程api工程(主要是寫一些實體和接口)
pom文件如下
springbootDubbo wyb 1.0-SNAPSHOT 4.0.0 api org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.0.Final org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.0.Final org.apache.kafka kafka_2.11 0.10.0.0 org.slf4j slf4j-log4j12 com.oracle ojdbc6 11.2.0.1.0 org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.0.Final org.springframework.data spring-data-jpa 1.11.0.RELEASE
實體如下(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 { ListgetBszn(); }
3.創建子工程provider工程(這里主要寫接口的具體實現),因此在pom中必須添加api依賴
注:這里不需要再寫實體
pom文件如下:
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文件配置如下:
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> 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{ }
實現類如下(注解必須添加,需要和后面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 ListgetBszn() { List list = bsznRepository.findAll(); return list; } }
最后是創建子工程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
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() { Listlist = 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案例就已搭建完成,這里只寫了通過數據庫進行查詢。。。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/76988.html
摘要:本使用創建本地服務器,在就能完成全部流程,并不需要線上服務器。路徑要與后端接口一致。后端返回成功后,前端數據中對應的元素也要刪掉,更新視圖。控制器里拿一個方法出來說一下吧,完整的代碼都在。讀取操作完成后調用釋放連接。 寫在前面 本文只是本人學習過程的一個記錄,并不是什么非常嚴謹的教程,希望和大家一起共同進步。也希望大家能指出我的問題。適合有一定基礎,志在全棧的前端初學者學習,從點擊按鈕...
閱讀 2000·2023-04-25 16:53
閱讀 1442·2021-10-13 09:39
閱讀 606·2021-09-08 09:35
閱讀 1639·2019-08-30 13:03
閱讀 2121·2019-08-30 11:06
閱讀 1831·2019-08-30 10:59
閱讀 3188·2019-08-29 17:00
閱讀 2288·2019-08-23 17:55