一举庶、Spring使用過程中的踩坑記錄
- Spring通過注解使用多數(shù)據(jù)源
坑:@Autowired 按 byType 自動(dòng)注入,而 @Resource 則默認(rèn)按 byName 自動(dòng)注入觅玻,@Primary是優(yōu)先選擇疮方。
例如悍募,在項(xiàng)目中是有兩個(gè)Redis源,這兩個(gè)Redis Bean分別為dataRedisTemplate和redisTemplate常侦。
Redis Bean1:dataRedisTemplate浇冰,clusterNodes為${data-redis.cluster.nodes}
@Bean(name = "dataRedisTemplate")
public RedisTemplate dataRedisTemplate() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(sessionLettuceConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
// factory
@Resource
@Qualifier(value = "dataLettuceConnectionFactory")
private RedisConnectionFactory dataLettuceConnectionFactory;
// clusterNodes
@Value("${spring.data-redis.cluster.nodes}")
private String clusterNodes;
Redis Bean2:redisTemplate予弧,clusterNodes為${redis.cluster.nodes}
@Primary
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
// factory
@Resource
@Qualifier(value = "lettuceConnectionFactory")
private RedisConnectionFactory lettuceConnectionFactory;
// clusterNodes
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
在一個(gè)應(yīng)用中要把數(shù)據(jù)放入到“Redis Bean1:dataRedisTemplate”對(duì)應(yīng)的Redis中,于是我在這個(gè)應(yīng)用中使用方式如下:
@Autowired
private RedisTemplate dataRedisTemplate;
// 根據(jù)key獲取數(shù)據(jù)
Object obj = dataRedisTemplate.opsForValue().get(key);
實(shí)際上使用的是“Redis Bean2:redisTemplate”對(duì)應(yīng)的Redis湖饱。
破解方式一:把@Autowired換成@Resource 注解掖蛤。如下:
@Autowired
private RedisTemplate dataRedisTemplate;
// 把@Autowired換成@Resource
@Resource
private RedisTemplate dataRedisTemplate;
@Autowired和@Resource最大的區(qū)別就是:@Autowired 按 byType 自動(dòng)注入,而 @Resource 則默認(rèn)按 byName 自動(dòng)注入井厌。
這里還需要注意一個(gè)注解@Primary蚓庭,官方的說明如下:
Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.
@Primary 優(yōu)先方案,被注解的實(shí)現(xiàn)仅仆,優(yōu)先被注入
通常情況下@Autowired是通過byType的方法注入的器赞,可是在多個(gè)實(shí)現(xiàn)類的時(shí)候,byType的方式不再是唯一墓拜,而需要通過byName的方式來注入港柜,而這個(gè)name默認(rèn)就是根據(jù)變量名來的。
也就是說咳榜,如果沒有在redisTemplate()上面增加@Primary的話是沒有問題的夏醉,因?yàn)橛卸鄠€(gè)實(shí)現(xiàn)時(shí),@Autowired是會(huì)通過byName的方式來注入的涌韩,但是按照上面說的畔柔,因?yàn)橛辛薂Primary,@Autowired注解會(huì)優(yōu)先使用Bean redisTemplate臣樱。
還有一種解決方案是增加@Qualifier(value = "dataRedisTemplate")靶擦,如下:
@Autowired
private RedisTemplate dataRedisTemplate;
// 換成:增加@Qualifier(value = "dataRedisTemplate")
@Autowired
@Qualifier(value = "dataRedisTemplate")
private RedisTemplate dataRedisTemplate;
- Spring事務(wù)@Transactional失效問題
坑:若同一類中的其他沒有@Transactional 注解的方法內(nèi)部調(diào)用有@Transactional 注解的方法,有@Transactional 注解的方法的事務(wù)被忽略雇毫,不會(huì)發(fā)生回滾玄捕。
FooService.class
public interface FooService {
void insertRecord();
void insertThenRollback() throws Exception;
void invokeInsertThenRollback() throws Exception;
void invokeInsertThenRollbackTwo() throws Exception;
}
FooServiceImpl.class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private FooService fooService;
@Override
@Transactional
public void insertRecord() {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('AAA')");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void insertThenRollback() throws Exception {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new Exception();
}
@Override
public void invokeInsertThenRollback() throws Exception {
insertThenRollback();
}
@Override
public void invokeInsertThenRollbackTwo() throws Exception {
fooService.insertThenRollback();
}
}
執(zhí)行
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement(mode = AdviceMode.PROXY)
@Slf4j
public class DeclarativeTransactionDemoApplication implements CommandLineRunner {
@Autowired
private FooService fooService;
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(DeclarativeTransactionDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// 標(biāo)記1:輸出 AAA 1
fooService.insertRecord();
log.info("AAA {}",jdbcTemplate.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='AAA'", Long.class));
// 標(biāo)記2:輸出 BBB 0,事務(wù)生效
try {
fooService.insertThenRollback();
} catch (Exception e) {
log.info("BBB {}",jdbcTemplate.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}
// 標(biāo)記3:輸出 BBB 1棚放,事務(wù)未生效
// ***這個(gè)地方就是最容易踩坑的地方C墩场!席吴!***
try {
fooService.invokeInsertThenRollback();
} catch (Exception e) {
log.info("BBB {}",jdbcTemplate.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}
// 標(biāo)記4:輸出 BBB 1赌结,事務(wù)生效(如果把標(biāo)記3的代碼注釋掉,輸出 BBB 0)
// ***這是避免踩坑的一種方式***
try {
fooService.invokeInsertThenRollbackTwo();
} catch (Exception e) {
log.info("BBB {}",jdbcTemplate.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}
}
}
二孝冒、RocketMQ使用過程中的踩坑記錄
-
Rocket默認(rèn)開啟了VIP通道導(dǎo)致10909failed問題
坑:Rocket默認(rèn)開啟了VIP通道柬姚,VIP通道端口為10911-2=10909。若Rocket服務(wù)器未啟動(dòng)端口10909庄涡,則報(bào)connect to <:10909> failed量承。
解決方案:不走VIP通道。
producer.setVipChannelEnabled(false);
consumer.setVipChannelEnabled(false);
- Rocket instanceName參數(shù)未做配置導(dǎo)致重復(fù)消費(fèi)問題
坑:一個(gè)是Rocket如果沒有配置instanceName,那么會(huì)使用pid做instanceName撕捍,如果instanceName一樣會(huì)重復(fù)消費(fèi)拿穴,因?yàn)榧合M(fèi)模式是按instanceName做為唯一消費(fèi)實(shí)例。
查看源碼忧风,如果沒有指定instanceName默認(rèn)會(huì)把pid做為instanceName默色,如下:
if (this.instanceName.equals("DEFAULT")) {
this.instanceName = String.valueOf(UtilAll.getPid());
}
public static int getPid() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
String name = runtime.getName(); // format: "pid@hostname"
try {
return Integer.parseInt(name.substring(0, name.indexOf('@')));
}
catch (Exception e) {
return -1;
}
}
解決方案:運(yùn)維配置有$MQ_INSTANCE_NAME
環(huán)境變量,不同機(jī)器不一樣狮腿,所以可以使用:mq.consumer.instanceName:${MQ_INSTANCE_NAME:默認(rèn)值}
進(jìn)行配置腿宰。
@Value("${rocketmq.consumer.instanceName:${MQ_INSTANCE_NAME:fota}}")
private String clientInstanceName;
consumer.setInstanceName(clientInstanceName);