更新时间:2024-09-05 GMT+08:00
使用Rainbow SDK(Spring Cloud框架)
引入Rainbow SDK
- Rainbow SDK依赖Cloud Map的注册和发现能力,在引入Rainbow SDK之前,要先引入STS SDK和Cloud Map SDK,并完成STS和Cloud Map的初始化,具体请参见使用STS SDK(Spring Cloud框架)和使用Cloud Map SDK(Spring Cloud框架)。
使用Cloud Map进行数据库注册和发现时,需要先在WiseDBA管理台配置连接信息,并注册到Cloud Map中,Rainbow SDK连接数据库的过程中,直接调用Cloud Map中的注册信息,具体操作请参见新增SDK配置。
- 引入Rainbow SDK。
<dependency> <groupId>com.huawei.wisecloud.nuwa</groupId> <artifactId>rainbow-proxy</artifactId> <version>${rainbow-sdk-version}</version> </dependency>
- 将${rainbow-sdk-version}替换成实际所使用的Rainbow SDK版本。
- 如果将SDK放到外部maven仓中,则只需要添加rainbow-proxy依赖。
- 如果采用本地依赖的方式引入SDK,即手动将本地下载的SDK jar包引入到工程的lib目录下,还需要添加间接依赖:rainbow-core-drds、gpaas-middleware-common。
初始化Rainbow SDK
代码如下:
package com.huawei.demo.serviceb.config; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Import; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.support.TransactionTemplate; import com.huawei.nuwa.map.client.NuwaMapClient; import com.huawei.nuwa.map.spring.boot.autoconfigure.NuwaCloudMapAutoConfiguration; import com.huawei.rainbow.jdbc.DbGroupDataSource; import com.huawei.wisesecurity.sts.springboot.security.configuration.StsEncryptablePropertiesConfiguration; /** * Rainbow启动 */ @Configuration @Order(Ordered.HIGHEST_PRECEDENCE) @Import(value = {NuwaCloudMapAutoConfiguration.class, StsEncryptablePropertiesConfiguration.class}) public class ApplicationAutoConfig implements EnvironmentAware { private Environment environment; /** * Rainbow需要依赖STS和Cloud Map启动,创建dataSource * * @param client NuwaMapClient值对象 * @return DataSource 初始化完成的数据源数据源 */ @Bean @DependsOn({"stsBootStrap"}) public DataSource createDataSource(NuwaMapClient client) { DbGroupDataSource dataSource = new DbGroupDataSource(); dataSource.setUseSts(true); dataSource.setAppName(environment.getProperty("wiseDba.appName")); dataSource.setDbGroupKey(environment.getProperty("wiseDba.groupKey")); dataSource.setDbName(environment.getProperty("wiseDba.dbName")); dataSource.init(); return dataSource; } /** * 事务管理创建 * * @param dataSource 数据源 * @return DataSourceTransactionManager 事务管理 */ @Bean public DataSourceTransactionManager createTransaction(DataSource dataSource) { DataSourceTransactionManager transaction = new DataSourceTransactionManager(); transaction.setDataSource(dataSource); return transaction; } @Bean public JdbcTemplate createJdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean("sqlSessionFactoryBean") public SqlSessionFactoryBean createMybatisSqlSessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); // 数据源配置项 org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); // 允许jdbc自动生成主键 configuration.setUseGeneratedKeys(true); // 使用列标签代替列名 configuration.setUseColumnLabel(true); // 打开下划线命名自动转换为驼峰命名开关 configuration.setMapUnderscoreToCamelCase(true); sessionFactoryBean.setConfiguration(configuration); return sessionFactoryBean; } @Bean public TransactionTemplate createTransactionTemplate(DataSourceTransactionManager dataSourceTransactionManager) { TransactionTemplate template = new TransactionTemplate(); template.setTransactionManager(dataSourceTransactionManager); return template; } @Bean public MapperScannerConfigurer createMapperScannerConfigurer() { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.huawei.demo"); configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); return configurer; } @Override public void setEnvironment(Environment environment) { this.environment = environment;
父主题: Rainbow SDK