@Configuration
@ComponentScan
@EnableTransactionManagement //自动开启事物管理
public class DataSourceTransactionManager {
/**
* 自定义事务
* MyBatis自动参与到Spring事务管理中,无需额外配置,只要org.mybatis.spring.SqlSessionFactoryBean引用的数据源与DataSourceTransactionManager引用的数据源一致即可,否则事务管理会不起作用。
* @return
*/
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(10000);
return userTransactionImp;
}
@Bean(name="userTransactionManager", destroyMethod = "close", initMethod = "init")
public UserTransactionManager userTransactionManager() {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
@Bean(name = "transactionManager")
@DependsOn({ "userTransaction", "userTransactionManager" })
public JtaTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
JtaTransactionManager manager = new JtaTransactionManager(userTransaction,userTransactionManager());
return manager;
}
}
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private PersonService personService;
@Transactional
public void handleInMultiDataBase(){
Person person = new Person();
person.setBirthDate(new Date());
person.setFirstName("tian");
person.setLastName("dd");
person.setPhoneNo("222");
person.setSex('F');
personService.insertPerson(person);
Person p = personService.getPersonById(1);
logger.info("handleInMultiDataBase p={}", JSON.toJSONString(p));
throw new RuntimeException("测试多数据源数据处理异常事物回滚");
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PersonServiceTest{
@Autowired
MultiDataBaseService multiDataBaseService;
@Test
public void testInsertPerson(){
Person person = new Person();
person.setBirthDate(new Date());
person.setFirstName("tian");
person.setLastName("dd");
person.setPhoneNo("111");
person.setSex('F');
personService.insertPerson(person);
}
}
通过测试方法发现本条记录并没有插入到主数据库中,说明JTA事物配置成功。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@Component
public class HealthCheck implements HealthIndicator{
@Override
public Health health() {
//自定义监控项目,实际情况扩充...
return Health.up().withDetail("health_check", true).build();
}
}
编译&部署使用如下命令即可:
mvn clean package -Dskiptests
java -jar target/spring-boot.jar --spring.profiles.active=test
#根据系统环境有dev/test/prod
服务启动成功后,就可以直接访问接口了。如Controller层代码如下:
@RestController
@EnableAutoConfiguration
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/person/getPersonById")
@ResponseBody
public JsonResult getPersonById(@RequestParam("id") Integer id){
return new JsonResult(personService.getPersonById(id));
}
}
服务配置如下:
#配置监控项
server:
port: 8082
management:
port: 9999
health:
mail:
enabled: false
security:
enabled: false
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
{
code: "200",
message: "成功",
data: {
id: 11,
firstName: "tian",
lastName: "dd_slave",
birthDate: 1510243200000,
sex: "F",
phoneNo: "111"
}
}
{
status: "UP",
healthCheck: {
status: "UP",
health_check: true
},
diskSpace: {
status: "UP",
total: 120108089344,
free: 56391913472,
threshold: 10485760
},
db: {
status: "UNKNOWN"
}
}
可以看到系统自定义的监控检查项也返回了。当然Spring Boot健康检查项还有很多API接口,如:info,beans,error等
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.2</version>
</dependency>
配置文件:
spring:
application:
name: spring-boot-admin
boot:
admin:
context-path: /sba # 配置访问路径为:http://localhost:8888/spring-boot-admin/sba
notify:
mail:
to: example@example.com
mail:
host: smtp.example.com
server:
port: 8888
context-path: /spring-boot-admin/ #统一为访问的url加上一个前缀
启动类:
@SpringBootApplication
@EnableScheduling
@EnableAdminServer //一定要加上
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
二、配置客户端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.2</version>
</dependency>
配置:
spring:
profiles:
active: dev
boot:
admin:
client:
prefer-ip: true
url: http://localhost:8888/spring-boot-admin # 向服务端注册的地址
先后启动服务端和客户端后,访问http://localhost:8888/spring-boot-admin/sba链接可查看到被监控的Spring Boot微服务。