SpringCloud学习之旅06--weather项目集成quartz(定时器)和Redis和theleaf模板引擎

目录结构:


该问只列举出和上篇博文不同的内容:

build.gradle:

// buildscript 代码块中脚本优先执行
buildscript {


// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M4'
}


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}


// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'


// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8


// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}


// 依赖关系
dependencies {


// 该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')


// HttpClient
compile('org.apache.httpcomponents:httpclient:4.5.3')


// Redis
compile('org.springframework.boot:spring-boot-starter-data-redis')


// Quartz
compile('org.springframework.boot:spring-boot-starter-quartz')


// 添加 Spring Boot Thymeleaf Starter 的依赖
compile('org.springframework.boot:spring-boot-starter-thymeleaf')


// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')

}



不同在于多了一个天气报表类项目的:

package com.waylau.spring.cloud.weather.service;


import com.waylau.spring.cloud.weather.vo.Weather;


/**
 * Weather Report Service.
 * 
 * @since 1.0.0 2017年11月24日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public interface WeatherReportService {


/**
* 根据城市ID查询天气信息
* @param cityId
* @return
*/
Weather getDataByCityId(String cityId);

}





package com.waylau.spring.cloud.weather.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.waylau.spring.cloud.weather.vo.Weather;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;


/**
 * Weather Report Service.
 * 
 * @since 1.0.0 2017年11月24日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
@Autowired
private WeatherDataService  weatherDataService;

@Override
public Weather getDataByCityId(String cityId) {
WeatherResponse resp = weatherDataService.getDataByCityId(cityId);
return resp.getData();
}


}






package com.waylau.spring.cloud.weather.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import com.waylau.spring.cloud.weather.service.CityDataService;
import com.waylau.spring.cloud.weather.service.WeatherReportService;
/**
 * Weather Report Controller.
 * 
 * @since 1.0.0 2017年11月24日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
@RequestMapping("/report")
public class WeatherReportController {
@Autowired
private CityDataService cityDataService;

@Autowired
private WeatherReportService weatherReportService;

/**
* http://localhost:8080/report/cityId/101281902
* @param cityId
* @param model
* @return
* @throws Exception
*/
@GetMapping("/cityId/{cityId}")
public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception {
model.addAttribute("title", "老卫的天气预报");
model.addAttribute("cityId", cityId);
model.addAttribute("cityList", cityDataService.listCity());
model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
return new ModelAndView("weather/report", "reportModel", model);
}


}





页面:report.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"
crossorigin="anonymous">


<title>老卫的天气预报(waylau.com)</title>
</head>
<body>
<div class="container">
<div class="row">
<h3 th:text="${reportModel.title}">waylau</h3>
<select class="custom-select" id="selectCityId">
<option th:each="city : ${reportModel.cityList}"
th:value="${city.cityId}" th:text="${city.cityName}"
th:selected="${city.cityId eq reportModel.cityId}"></option>
</select>
</div>
<div class="row">
<h1 class="text-success" th:text="${reportModel.report.city}">深圳</h1>
</div>
<div class="row">
<p>
空气质量指数:<span th:text="${reportModel.report.aqi}"></span>
</p>
</div>
<div class="row">
<p>
当前温度:<span th:text="${reportModel.report.wendu}"></span>
</p>
</div>
<div class="row">
<p>
温馨提示:<span th:text="${reportModel.report.ganmao}"></span>
</p>
</div>
<div class="row">
<div class="card border-info" th:each="forecast : ${reportModel.report.forecast}">
<div class="card-body text-info">
<p class ="card-text" th:text="${forecast.date}">日期</p>
<p class ="card-text" th:text="${forecast.type}">天气类型</p>
<p class ="card-text" th:text="${forecast.high}">最高温度</p>
<p class ="card-text" th:text="${forecast.low}">最低温度</p>
<p class ="card-text" th:text="${forecast.fengxiang}">风向</p>
</div>
</div>
</div>
</div>






<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"
integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh"
crossorigin="anonymous"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"
integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
crossorigin="anonymous"></script>
<!-- Optional JavaScript -->
<script type="text/javascript" th:src="@{/js/weather/report.js}"></script>
</body>

</html>



report.js:

/**
 * report页面下拉框事件
 * auther:waylau.com
 */
$(function(){
$("#selectCityId").change(function(){
var cityId = $("#selectCityId").val();
var url = '/report/cityId/'+ cityId;
window.location.href = url;
})

});



application.properties:

# 热部署静态文件

spring.thymeleaf.cache=false



最后启动项目访问:

http://localhost:8080/report/cityId/101280101



至此单块项目就已经完成了。这里面包括了,获取城市列表、获取天气数据、显示报表页面给用户等。这些操作全在一个项目里面完成了,对应维护和扩展不是很友好,接下来就引出了springcloud项目。

springcloud个人理解,就是把复杂一点的单块项目,分离成多个简单的单块项目,最终将简单的这些单块项目集成到一块,就是一个完整的项目了。

单个简单的独立模块就是一个微服务,由springcloud集成在一起,就是分布式系统了。(纯属个人理解,勿采纳!)



猜你喜欢

转载自blog.csdn.net/qq_33371766/article/details/80654191