成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

搭建簡(jiǎn)單的dubbo案例,并實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的增刪改查

MageekChiu / 1013人閱讀

摘要:創(chuàng)建父工程文件如下這里只添加幾個(gè)最簡(jiǎn)單的依賴啟動(dòng)父依賴依賴依賴依賴創(chuàng)建子工程

1.創(chuàng)建父工程

pom文件如下(這里只添加幾個(gè)最簡(jiǎn)單的依賴)



    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.創(chuàng)建子工程api工程(主要是寫一些實(shí)體和接口)

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
        
    


實(shí)體如下(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.創(chuàng)建子工程provider工程(這里主要寫接口的具體實(shí)現(xiàn)),因此在pom中必須添加api依賴

注:這里不需要再寫實(shí)體

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(這里是不需要配置注冊(cè)中心,直接使用組播協(xié)議,相當(dāng)于本地使用)
    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

啟動(dòng)類如下

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.需要先定義一個(gè)convert轉(zhuǎn)換消息的對(duì)象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json數(shù)據(jù);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3.在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4.將convert添加到converters中
        converters.add(fastConverter);

    }
}

dao層代碼如下(這里只做簡(jiǎn)單的查詢,所以繼承了JpaRepository后無需再寫方法)

package org.spring.springboot.dao;

import org.spring.springboot.domain.Bszn;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BsznRepository extends JpaRepository {

}

實(shí)現(xiàn)類如下(注解必須添加,需要和后面consumer做對(duì)應(yīng))

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;
    }
}

最后是創(chuàng)建子工程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的注冊(cè)地址和provider中的配置相同,不然會(huì)找不到)

server:
  port:  8081

spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: multicast://224.5.6.7:1234
    scan: org.spring.springboot.dubbo

啟動(dòng)類如下

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) {
        // 程序啟動(dòng)入口
        // 啟動(dòng)嵌入式的 Tomcat 并初始化 Spring 環(huán)境及其各 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;
            }
        }
    }
}

至此,簡(jiǎn)單的dubbo案例就已搭建完成,這里只寫了通過數(shù)據(jù)庫進(jìn)行查詢。。。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/76988.html

相關(guān)文章

  • 邁出全棧第一步,vue+node+mysql獨(dú)立完成前后端分離刪改流程

    摘要:本使用創(chuàng)建本地服務(wù)器,在就能完成全部流程,并不需要線上服務(wù)器。路徑要與后端接口一致。后端返回成功后,前端數(shù)據(jù)中對(duì)應(yīng)的元素也要?jiǎng)h掉,更新視圖。控制器里拿一個(gè)方法出來說一下吧,完整的代碼都在。讀取操作完成后調(diào)用釋放連接。 寫在前面 本文只是本人學(xué)習(xí)過程的一個(gè)記錄,并不是什么非常嚴(yán)謹(jǐn)?shù)慕坛?,希望和大家一起共同進(jìn)步。也希望大家能指出我的問題。適合有一定基礎(chǔ),志在全棧的前端初學(xué)者學(xué)習(xí),從點(diǎn)擊按鈕...

    fsmStudy 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<