溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Servlet+MyBatis項目如何轉(zhuǎn)Spring Cloud微服務(wù)

發(fā)布時間:2021-08-06 10:32:22 來源:億速云 閱讀:171 作者:小新 欄目:編程語言

這篇文章主要介紹了Servlet+MyBatis項目如何轉(zhuǎn)Spring Cloud微服務(wù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一、項目需求

在開發(fā)過程中,由于技術(shù)的不斷迭代,為了提高開發(fā)效率,需要對原有項目的架構(gòu)做出相應(yīng)的調(diào)整。

二、存在的問題

為了不影響項目進(jìn)度,架構(gòu)調(diào)整初期只是把項目做了簡單的maven管理,引入springboot并未做spring cloud微服務(wù)處理。但隨著項目的進(jìn)一步開發(fā),急需拆分現(xiàn)有業(yè)務(wù),做微服務(wù)處理。因此架構(gòu)上的短板日益突出。spring cloud config 無法完全應(yīng)用,每次項目部署需要修改大量配置文件。嚴(yán)重影響開發(fā)效率,因此便萌生了對項目架構(gòu)再次調(diào)整的決心。

三、調(diào)整建議

為了兼容以前的代碼版本,盡量不修改現(xiàn)有的代碼結(jié)構(gòu),以免增加額外的工作量并且為了更好的應(yīng)用cloud config。

首先,創(chuàng)建JdbcConfigBean類,用以讀取配置文件,實例代碼入如下(僅供參考):

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@RefreshScope
@Component("jdbcConfigBean")
public class JdbcConfigBean {
  @Value("${jdbc.driver}")
  private String driver;
  @Value("${db1.jdbc.url}")
  private String url;
  @Value("${db1.jdbc.username}")
  private String username;
  @Value("${db1.jdbc.password}")
  private String password;
  @Value("${db2.jdbc.url}")
  private String db2_url;
  @Value("${db2.jdbc.username}")
  private String db2_username;
  @Value("${db2.jdbc.password}")
  private String db2_password;
  // 其他數(shù)據(jù)源
  // 其他配置
  // 相應(yīng)的getter setter 方法
}

其次,創(chuàng)建數(shù)據(jù)源,代碼示例如下(僅供參考):

/**
 * xml Mabatis XML配置文件
 * @param xml
 * @return
 **/
 public static SqlSessionFactory create(String xml){
 JdbcConfigBean jdbcConfigBean = (JdbcConfigBean) SpringContextUtil.getBean("jdbcConfigBean");
 Properties properties = new Properties();
 properties.setProperty("jdbc.driver", jdbcConfigBean.getDriver());
 properties.setProperty("db1.jdbc.url", jdbcConfigBean.getUrl());
 properties.setProperty("db1.jdbc.username", jdbcConfigBean.getUsername());
 properties.setProperty("db1.jdbc.password", jdbcConfigBean.getPassword());
 properties.setProperty("db2.virtual.jdbc.url", jdbcConfigBean.getDb2_url());
 properties.setProperty("db2.virtual.jdbc.username", jdbcConfigBean.getDb2_username());
 properties.setProperty("db2.virtual.jdbc.password", jdbcConfigBean.getDb2_password());
 // 其他屬性
 try {
  Reader reader = Resources.getResourceAsReader(xml);
  SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
  return sqlSessionFactoryBuilder.build(reader, properties);
 } catch (IOException e) {
  log.error("創(chuàng)建數(shù)據(jù)源失敗:" + e.getMessage());
 }
 return null;
 }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Servlet+MyBatis項目如何轉(zhuǎn)Spring Cloud微服務(wù)”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI