学成在线 第9天 讲义-课程预览 Eureka Feign
配置文件大部分情况也是复制的,基本上没有什么难度,也没有必要开发
接下来是导入单机的环境,建议大家直接复制代码,这个代码真心没有敲的必要,但是这个项目最后又一个部署的案例,这个可以学习一下,前期的这些代码,根据视频说讲的,可读性真的很差
高可用
在各个微服务中导入eureka client的客户端坐标,在客户端启动类加入注解
@EnableDiscoveryClient //一个EurekaClient从EurekaServer发现服务
这个客户端加入的步骤是,添加依赖,修改配置文件,给启动类添加注解
这个负载均衡的测试,其实之前的乐友的项目中都有描述,而且讲的比这个要好很多,这个项目基本都在复制粘贴代码,大部分自己动手写的情况很少,前端代码全部都写好了,基本就是复制讲义中的代码,然后运行一下
给启动类添加bena
完成测试
3.2 课程详情页面技术方案
这个部分还是复制粘贴代码,不领者再写了,这个项目的重点是微服务的部署
下面是nginx的配置信息
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#cms页面预览
upstream cms_server_pool{
server 127.0.0.1:31001 weight=10;
}
upstream img_server_pool{
server 192.168.1.99 weight=10;
}
#静态资源服务
upstream static_server_pool{
server 127.0.0.1:91 weight=10;
}
server{
listen 80;
server_name www.xuecheng.com;
ssi on;
ssi_silent_errors on;
location / {
alias D:/IDEA_work/xc_edu_online/xc_ui_portal/xc-ui-pc-static-portal/;
index index.html;
}
location /cms/preview {
proxy_pass http://cms_server_pool/preview/;
}
location /static/img/ {
alias D:/IDEA_work/xc_edu_online/xc_ui_portal/xc-ui-pc-static-portal/img/;
}
location /static/css/ {
alias D:/IDEA_work/xc_edu_online/xc_ui_portal/xc-ui-pc-static-portal/css/;
}
location /static/js/ {
alias D:/IDEA_work/xc_edu_online/xc_ui_portal/xc-ui-pc-static-portal/js/;
}
location /static/plugins/ {
alias D:/IDEA_work/xc_edu_online/xc_ui_portal/xc-ui-pc-static-portal/plugins/;
add_header Access‐Control‐Allow‐Origin http://ucenter.xuecheng.com; add_header Access‐Control‐Allow‐Credentials true;
add_header Access‐Control‐Allow‐Methods GET;
}
}
server {
listen 80;
server_name img.xuecheng.com;
location /group1 {
proxy_pass http://img_server_pool;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#学成网静态资源
server { listen 91;
server_name localhost;
#公司信息
location /static/company/ {
alias D:/IDEA_work/xc_edu_online/static/company/;
}
#老师信息
location /static/teacher/ {
alias D:/IDEA_work/xc_edu_online/static/teacher/;
}
#统计信息
location /static/stat/ {
alias D:/IDEA_work/xc_edu_online/static/stat/;
}
location /course/detail/ {
alias D:/IDEA_work/xc_edu_online/static/course/detail/;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
CourseView的接口的开发,还是Controller&Service哪一套
Service:
public CourseView getCoruseView(String id) {
CourseView courseView=new CourseView();
//查询课程基本信息
Optional<CourseBase> courseBase = courseBaseRepository.findById(id);
if (courseBase.isPresent()){
CourseBase course = courseBase.get();
courseView.setCourseBase(course);
}
//查询课程的图片信息
Optional<CoursePic> coursePic = coursePicRepository.findById(id);
if (coursePic.isPresent()){
CoursePic pic = coursePic.get();
courseView.setCoursePic(pic);
}
//查询课程的营销信息
Optional<CourseMarket> courseMarket = courseMarketRepository.findById(id);
if (courseMarket.isPresent()){
CourseMarket courseMarket1 = courseMarket.get();
courseView.setCourseMarket(courseMarket1);
}
//查询课程的计划信息
TeachplanNode teachplanNode = teachplanMapper.selectList(id);
courseView.setTeachplanNode(teachplanNode);
return courseView;
}
静态化模板
@RequestMapping("/course")
public String course(Map<String, Object> map){
//使用restTemplate请求轮播图的模型数据
ResponseEntity<Map> forEntity = restTemplate.getForEntity("http://localhost:31200/course/courseview/4028e581617f945f01617f9dabc40000", Map.class);
Map body = forEntity.getBody();
//设置模型数据
map.putAll(body);
return "course";
}
PS:这个图片在加载的时候又个巨坑,第一个坑就是这个图片的地址因为是在fastFDS中存的所以我的FastFDS中没这个图片,这个pic识别不到。页面会报404,第二个坑就是controller代码写的又问题,造成没有执行静态化,直接区读取模板信息了,这个第privew/pageID, 最后就是这个nginx 最好不要重新加载,从任务管理器中停止掉,直接重写启动一个,接下来就这个模板和模板数据的关系的问题,首先就是得现有模板就是course.ftl,然后把这个course.ftl,通过测试类存到,GridFs中,这样就又一个得到Grid得id,然后把这个GridFs就是teamplate表中得文件得ID,这个时候,也有配置一个stie_id,这个可以自己新增,这个因为他得库里面没有,所以会出问题,这样这个模板就生成了,然后在cms_page中就配置相应得site_id,和template_id,这个没有新增模板得操作,所以加模板操作起来异常得麻烦,再次感觉这个项目是真得很垃圾!!!!
Service:
//保存页面
public CmsPageResult save(CmsPage cmsPage) {
CmsPageResult cmsPageResult = cmsPageRepository.findBySiteIdAndPageWebPathAndPageName(cmsPage.getPageName(), cmsPage.getPageWebPath(), cmsPage.getSiteId());
if (cmsPageResult!=null){
return this.edit(cmsPage.getPageId(),cmsPage);
}
return this.insertCmsPage(cmsPage);
}
大部分情况都是在复制代码!!!!
课程得Service
public CoursePublishResult preview(String id) {
//查询课程
CourseBase courseBaseById = this.findCourseBaseById(id);
//请求cms添加页面
//准备cmsPage信息
CmsPage cmsPage = new CmsPage();
cmsPage.setSiteId(publish_siteId);//站点id
cmsPage.setDataUrl(publish_dataUrlPre+id);//数据模型url
cmsPage.setPageName(id+".html");//页面名称
cmsPage.setPageAliase(courseBaseById.getName());//页面别名,就是课程名称
cmsPage.setPagePhysicalPath(publish_page_physicalpath);//页面物理路径
cmsPage.setPageWebPath(publish_page_webpath);//页面webpath
cmsPage.setTemplateId(publish_templateId);//页面模板id
//远程调用cms
CmsPageResult cmsPageResult = cmsPageClient.saveCmsPage(cmsPage);
if(!cmsPageResult.isSuccess()){
return new CoursePublishResult(CommonCode.FAIL,null);
}
CmsPage cmsPage1 = cmsPageResult.getCmsPage();
String pageId = cmsPage1.getPageId();
//拼装页面预览的url
String url = previewUrl+pageId;
//返回CoursePublishResult对象(当中包含了页面预览的url)
return new CoursePublishResult(CommonCode.SUCCESS,url);
}
完成验证
这个里面又个很深得坑前端得找个页面得找个test01找个地方是写死得,怀疑是故意给坑得,注意要生成连接得时候记得把course得ID写道下面,不然会没有反应,后台代码没有问题找个地方,找到填充进去就可以了,不然一直404