资讯专栏INFORMATION COLUMN

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

MageekChiu / 969人阅读

摘要:创建父工程文件如下这里只添加几个最简单的依赖启动父依赖依赖依赖依赖创建子工程

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

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76988.html

相关文章

  • 迈出全栈第一步,vue+node+mysql独立完成前后端分离删改流程

    摘要:本使用创建本地服务器,在就能完成全部流程,并不需要线上服务器。路径要与后端接口一致。后端返回成功后,前端数据中对应的元素也要删掉,更新视图。控制器里拿一个方法出来说一下吧,完整的代码都在。读取操作完成后调用释放连接。 写在前面 本文只是本人学习过程的一个记录,并不是什么非常严谨的教程,希望和大家一起共同进步。也希望大家能指出我的问题。适合有一定基础,志在全栈的前端初学者学习,从点击按钮...

    fsmStudy 评论0 收藏0

发表评论

0条评论

MageekChiu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<