目录
核心配置文件
多环境配置
整合redis
整合mybatis
整合Dubbo
事务管理
配置拦截器
读取配置文件
之前介绍了SpringBoot项目的初始搭建,这次我们尝试整合redis + mybatis + Dubbo 等中间件
SpringBoot中省略了xml的繁琐的配置,配置文件采用.properties或yml的文件配置,如下图所示
//配置tomcat端口号
server.port=9090
//配置上下文路径
server.servlet.context-path=/xxxx
//激活profile文件
spring.profiles.active=test
配置完毕后启动项目
可以为每一个环境配置一个配置文件,名称以application-xxx.properties为规范,在主文件中进行激活配置
application.properties
spring.profiles.active=test
application-test.properties
server.port=9090
server.servlet.context-path=/xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crm?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=xxxxx
spring.datasource.password=xxxx
启动项目后:可以看到激活的application-test.properties生效
1、pom文件中增加redis依赖
org.springframework.boot
spring-boot-starter-data-redis
2、配置redis属性
单节点配置
spring.redis.host=10.16.xxx.xxx
spring.redis.port=6379
哨兵方式配置
#主节点名称
spring.redis.sentinel.master=sentinel-xxxxx
#哨兵节点
spring.redis.sentinel.nodes=xxxxxxx:640x,xxxxx:640x,xxxxx:640x
SpringBoot中帮我们自动注入了RedisTemplate直接引入即可
使用:
1、项目中增加依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
2、增加数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crm?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=xxx
spring.datasource.password=xxx
生成Mapper及xml文件
Service下引用mapper
方法一:
Mapper上增加@Mapper (mybatis自动扫描数据持久层的映射文件及DAO接口的关系)
默认情况下,Mybatis的xml映射文件不会编译到target的class目录下,所以我们需要在pom.xml文件中配置resource
方式二:在运行的主类上添加注解包扫描@MapperScan("com.bjpowernode.springboot.mapper")
@MapperScan(value = "com.xxxx.crm.demo.mapper")
1、新建provider项目
2、项目中增加duboo依赖及zk依赖
com.alibaba.spring.boot
dubbo-spring-boot-starter
2.0.0
com.101tec
zkclient
0.10
3、引入api模板依赖
com.ziroom.crm
springboot-dubbo-api
0.0.1-SNAPSHOT
4.添加provider的dubbo配置
spring.application.name=springboot-dubbo-provider
#表示是服务提供者,可以省略
spring.dubbo.server=true
#注册中心地址
spring.dubbo.registry.address=xxxxxxxx:8081
spring.dubbo.registry.protocol=zookeeper
5、增加接口实现
6、主类上开启Dubbo配置支持注解
@SpringBootApplication
@EnableDubboConfiguration
public class SpringbootDubboWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDubboWebApplication.class, args);
}
}
7、consumer消费者引入jar包
com.ziroom.crm
springboot-dubbo-api
0.0.1-SNAPSHOT
8、controller中引入功能
@RequestMapping(value = "/hello")
@Controller
public class HelloController {
//@Reference注册是由阿里所提供
//@Reference注解 相当于
//如果服务提供者
@Reference(check = false)
private UserSerivce userSerivce;
@RequestMapping("/testDubbo")
@ResponseBody
public String testDubbo(@RequestBody JSONObject jsonObject){
String desc = jsonObject.getString("desc");
return userSerivce.getDesc(desc);
}
SpringBoot 使用事务非常简单,底层依然采用的是Spring本身提供的事务管理
• 在入口类中使用注解 @EnableTransactionManagement 开启事务支持
• 在访问数据库的Service方法上添加注解 @Transactional 即可
1、实现一个登录拦截器
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("登录拦截器");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
2、通过配置类注册拦截器
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Bean
LoginInterceptor getLoginInterceptor(){
return new LoginInterceptor();
}
@Autowired
private ApplicationContext applicationContext;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//定义需要拦截的路径
String [] addPathPatterns = {
//"/hello/**"
};
//定义不需要拦截的路径
String [] excludePathPatterns = {
"/hello/login",
"/hello/register",
"/hello/testConfig",
"/hello/testRedis",
"/hello/saveHello",
//"/hello/getHello",
"/hello/testDubbo"
};
//registry.addInterceptor(new LoginInterceptor())
registry.addInterceptor((LoginInterceptor)applicationContext.getBean("loginInterceptor"))
.addPathPatterns(addPathPatterns)
.excludePathPatterns(excludePathPatterns);
}
}
3.访问相关接口看是否拦截成功
1、添加配置读取类
@Component
@ConfigurationProperties(prefix = "school")
public class OrderConfig {
private String name;
private String webSite;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebSite() {
return webSite;
}
public void setWebSite(String webSite) {
this.webSite = webSite;
}
}
2、配置文件中添加属性值
application-test.properties
school.name=xxxxx
school.webSite=www.xxxx.com
3、controller中引用
@RequestMapping(value = "/hello")
@Controller
public class HelloController {
//方法一
@Value("${school.name}")
private String schoolName;
@Value("${school.webSite}")
private String schoolWebSite;
@RequestMapping("/testConfig")
@ResponseBody
public void test(){
System.out.println(schoolName);
System.out.println(orderConfig.getName());
System.out.println(orderConfig.getWebSite());
}
}
4、输出