一、需求分析
1.通过查询用户表中的创建时间字段,得到某天注册人数存入统计分析数据表(实际过程调用接口)
2.统计分析模块调用注册中心模块,把统计分析表里存储的数据,使用图片显示。
二、创建统计分析微服务
1.新建模块
2、application.properties
resources目录下创建文件
# 服务端口
server.port=8008
# 服务名
spring.application.name=service-statistics
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yunxun?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/yunxun/staservice/mapper/xml/*.xml
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
3、创建SpringBoot启动类
@SpringBootApplication
@MapperScan("com.yunxun.staservice.mapper")
@ComponentScan("com.yunxun")
@EnableDiscoveryClient //nacos 注册中心
@EnableFeignClients //远程调用--服务发现
public class StaApplication {
public static void main(String[] args) {
SpringApplication.run(StaApplication.class, args);
}
}
三、实现服务调用,生成统计某一天的注册人数数据
1、在service_ucenter模块创建接口,统计某一天的注册人数
controller
@GetMapping(value = "countregister/{day}")//查询某一天的注册人数
public R registerCount(
@PathVariable String day){
Integer count = memberService.countRegisterByDay(day);
return R.ok().data("countRegister", count);
}
service
@Override //查询某一天的注册人数
public Integer countRegisterByDay(String day) {
return baseMapper.selectRegisterCount(day);
}
mapper
<select id="selectRegisterCount" resultType="java.lang.Integer">
SELECT COUNT(1)
FROM ucenter_member
WHERE DATE(gmt_create) = #{
value}
</select>
2、在service_statistics模块创建远程调用接口
创建client包和UcenterClient接口
@Component
@FeignClient("service-ucenter")
public interface UcenterClient {
//查询某一天的注册人数
@GetMapping(value = "/eduucenter/member/countregister/{day}")
public R registerCount(@PathVariable("day") String day);
}
**StatisticsDailyController **
package com.atguigu.staservice.controller;
import com.atguigu.commonutils.R;
import com.atguigu.staservice.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* <p>
* 网站统计日数据 前端控制器
* </p>
*
* @author testjava
* @since 2020-03-14
*/
@RestController
@RequestMapping("/staservice/sta")
@CrossOrigin
public class StatisticsDailyController {
@Autowired
private StatisticsDailyService staService;
//统计某一天注册人数,生成统计数据
@PostMapping("registerCount/{day}")
public R registerCount(@PathVariable String day) {
staService.registerCount(day);
return R.ok();
}
}
StatisticsDailyServiceImpl
package com.atguigu.staservice.service.impl;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.bao