dubbo mysql实例_搭建简单的dubbo案例,并实现对数据库的增删改查

1.创建父工程

5fa6c10ea1fe30f84e79ea00d5dd72de.png

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

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工程(主要是写一些实体和接口)

46d127cd24cd4124f6afbe01c82b0092.png

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">

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 {

List getBszn();

}

3.创建子工程provider工程(这里主要写接口的具体实现),因此在pom中必须添加api依赖

5cf041c06a64ea6cc70516240aba1bbc.png

注:这里不需要再写实体

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文件配置如下:

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 List getBszn() {

List list = bsznRepository.findAll();

return list;

}

}

最后是创建子工程consumer工程

1d0274fde862a1a3f5b308ea42bd1d02.png

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">

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: 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 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案例就已搭建完成,这里只写了通过数据库进行查询。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值