apollo切换mysql到达梦数据库

当前项目中有要求需要适配多个数据库,于是需要对于apollo也需要适配多个数据库,因为apollo采用的是jpa而不是国内很多用的mybatis+mybatis plus,可能改造的地方有些不太一样,有些适配方案我是参考了这些链接,项目上的mysql迁移到DM默认schema、表名和字段名都会大写(目前apollo稳定版本是2.1.0):

apollo 适配达梦_apollo 达梦-CSDN博客

apollo配置中心源码针对相关数据源切换(mysql,oracle,达梦)_apollo 替换oracle-CSDN博客

参考完后我做的修改如下:

1. pom修改

主要是对于这两个pom文件修改,增加两个依赖:

        <!-- 达梦数据库 -->
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.dameng/DmDialect-for-hibernate5.6 -->
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmDialect-for-hibernate5.6</artifactId>
        </dependency>

2.修改对应application-github.properties文件

apollo中三个服务对应都会有application-github.properties文件,在这个文件中加入一行数据源驱动配置:

spring.datasource.driver-class-name=${spring_datasource_driver}

3.修改对应application.yaml文件

去掉默认的mysql初始化调试参数:

#  datasource:
#    hikari:
#      connectionInitSql: set names utf8mb4

4.修改AuthConfiguration.java、InstanceConfigRepository.java这两个文件

就是将`这个符号去掉,例如:

原始

 @Query(
      value = "select b.Id from `InstanceConfig` a inner join `Instance` b on b.Id =" +
          " a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
          ":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
          "> :validDate and b.`AppId` = :instanceAppId",
      countQuery = "select count(1) from `InstanceConfig` a inner join `Instance` b on b.id =" +
          " a.`InstanceId` where a.`ConfigAppId` = :configAppId and a.`ConfigClusterName` = " +
          ":clusterName and a.`ConfigNamespaceName` = :namespaceName and a.`DataChange_LastTime` " +
          "> :validDate and b.`AppId` = :instanceAppId",
      nativeQuery = true)

现在:

@Query(
      value = "select b.Id from InstanceConfig a inner join Instance b on b.Id =" +
          " a.InstanceId where a.ConfigAppId = :configAppId and a.ConfigClusterName = " +
          ":clusterName and a.ConfigNamespaceName = :namespaceName and a.DataChange_LastTime " +
          "> :validDate and b.AppId = :instanceAppId",
      countQuery = "select count(1) from InstanceConfig a inner join Instance b on b.id =" +
          " a.InstanceId where a.ConfigAppId = :configAppId and a.ConfigClusterName = " +
          ":clusterName and a.ConfigNamespaceName = :namespaceName and a.DataChange_LastTime " +
          "> :validDate and b.AppId = :instanceAppId",
      nativeQuery = true)

5.重写CamelCaseToUnderscoresNamingStrategy.java类

这个类是hibernate-core包下的,主要是针对schema、table、column等名称的转换,在之后的启动微服务中需要配置上对应的参数,要在这个

package org.hibernate.boot.model.naming;包下,在apollo-common这个模块添加,具体如下:
package org.hibernate.boot.model.naming;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

/**
 * Originally copied from Spring Boot as this strategy is popular there
 * (original name is SpringPhysicalNamingStrategy).
 *
 * @author Phillip Webb
 * @author Madhura Bhave
 */
public class CamelCaseToUnderscoresNamingStrategy implements PhysicalNamingStrategy {

//	private static final List<String> keywords = Arrays.asList("comment", "cluster", "reference", "percent");
	@Override
	public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		return apply( name, jdbcEnvironment );
	}

	@Override
	public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		return apply( name, jdbcEnvironment );
	}

	@Override
	public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		return apply( name, jdbcEnvironment );
	}

	@Override
	public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		return apply( name, jdbcEnvironment );
	}

	@Override
	public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
		return apply( name, jdbcEnvironment );
	}

	private Identifier apply(final Identifier name, final JdbcEnvironment jdbcEnvironment) {
		if ( name == null ) {
			return null;
		}
		/*StringBuilder builder = new StringBuilder( name.getText().replace( '.', '_' ) );
		for ( int i = 1; i < builder.length() - 1; i++ ) {
			if ( isUnderscoreRequired( builder.charAt( i - 1 ), builder.charAt( i ), builder.charAt( i + 1 ) ) ) {
				builder.insert( i++, '_' );
			}
		}
		return getIdentifier( builder.toString(), name.isQuoted(), jdbcEnvironment );*/
		return getIdentifier( name.getText().replace( '.', '_' ), name.isQuoted(), jdbcEnvironment );
	}

	/**
	 * Get an identifier for the specified details. By default this method will return an identifier
	 * with the name adapted based on the result of {@link #isCaseInsensitive(JdbcEnvironment)}
	 *
	 * @param name the name of the identifier
	 * @param quoted if the identifier is quoted
	 * @param jdbcEnvironment the JDBC environment
	 *
	 * @return an identifier instance
	 */
	protected Identifier getIdentifier(String name, final boolean quoted, final JdbcEnvironment jdbcEnvironment) {
		if ( isCaseInsensitive( jdbcEnvironment ) ) {
			name = name.toLowerCase( Locale.ROOT );
		}
		// 判断是否达梦,达梦大写
		if (jdbcEnvironment.getDialect().getClass().getName().equalsIgnoreCase("org.hibernate.dialect.DmDialect")) {
//			String finalName = name;
//			if (keywords.stream().anyMatch(str -> str.equalsIgnoreCase(finalName))) {
//				return new Identifier(name.toUpperCase(), true);
//			}
			return new Identifier(name.toUpperCase(), true);
		}
		return new Identifier( name, quoted );
	}

	/**
	 * Specify whether the database is case sensitive.
	 *
	 * @param jdbcEnvironment the JDBC environment which can be used to determine case
	 *
	 * @return true if the database is case insensitive sensitivity
	 */
	protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
		return true;
	}

	private boolean isUnderscoreRequired(final char before, final char current, final char after) {
		return Character.isLowerCase( before ) && Character.isUpperCase( current ) && Character.isLowerCase( after );
	}

}

6.启动参数

在apollo-adminservice apollo-configservice这两个服务启动时添加的VM参数如下:

-Dspring_datasource_driver=dm.jdbc.driver.DmDriver -Dapollo_profile=github -Dspring.datasource.url=jdbc:dm://129.0.0.1:30236?ema=APOLLOCONFIGDB_2_1_0&characterEncoding=utf8&compatibleMode=mysql&clobAsString=true -Dspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DmDialect -Dspring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy -Dspring.datasource.username=SYSDBA -Dspring.datasource.password=SYSDBA001 -Dspring.security.eureka.host=127.0.0.1 -Dspring.security.eureka.port=7761 

在apollo-portal这个服务启动时添加的VM参数如下:

-Dspring_datasource_driver=dm.jdbc.driver.DmDriver -Dapollo_profile=github,auth -Ddev_meta=http://localhost:8080/ -Dapollo.meta=http://localhost:8080/ -Dserver.port=8070 -Dspring.datasource.url=jdbc:dm://127.0.0.1:30236?schema=APOLLOPORTALDB_2_1_0&characterEncoding=utf8&characterEncoding=utf8&compatibleMode=mysql&clobAsString=true -Dspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DmDialect -Dspring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy  -Dspring.datasource.username=SYSDBA -Dspring.datasource.password=SYSDBA001

如果是用的apollo自带的eureka,则在参数中不需要配置eureka相关参数,最后执行启动就可以了

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Apollo中展示MySQL数据,可以按照以下步骤进行操作: 1. 在Apollo控制台中创建一个数据库配置,包括IP地址、端口号、用户名和密码等信息。 2. 在Apollo控制台中创建一个应用,选择MySQL作为配置中心的类型,并将第一步中创建的数据库配置与该应用关联。 3. 在应用的“配置管理”页面中,选择“MySQL配置”标签,点击“新增配置”按钮,输入配置项的key和value,保存配置。 4. 在代码中,通过Apollo客户端从配置中心获取MySQL配置,使用JDBC连接MySQL数据库,查询数据并展示。 下面是一个Java示例代码: ```java import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MysqlDemo { public static void main(String[] args) throws Exception { // 从Apollo配置中心获取MySQL配置 Config config = ConfigService.getAppConfig(); String username = config.getProperty("mysql.username", ""); String password = config.getProperty("mysql.password", ""); String url = config.getProperty("mysql.url", ""); // 使用JDBC连接MySQL数据库 Connection conn = DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM user"); // 展示MySQL数据 while (rs.next()) { System.out.println(rs.getString("id") + ", " + rs.getString("name") + ", " + rs.getString("age")); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } } ``` 在上述示例代码中,我们使用Apollo客户端从配置中心获取MySQL配置,然后使用JDBC连接MySQL数据库,查询并展示数据。通过这种方式,我们可以在Apollo中管理MySQL的配置,同时在应用中方便地使用这些配置,并展示MySQL数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值