查询天气预报系统之---使用Feign实现天气预报微服务(十一)

本文介绍了springBoot-report-feign项目的搭建过程,包括项目结构、pom文件、配置文件等内容,还提及了service接口及实现类、vo类等。启动该项目前需先启动redis、springBoot - City、springBoot - data,最后通过特定链接访问验证项目搭建成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建项目:springBoot-report-feign

项目结构:

 pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.csq.study</groupId>
  <artifactId>springBoot-report-feign</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.14.RELEASE</version>
	</parent>
	
	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<spring.cloud.version>Edgware.SR5</spring.cloud.version>
	    <quartz.version>2.0.0.RELEASE</quartz.version>
	</properties>
	
	<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
			</dependency>
			  <!-- Eureka服务-->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-eureka</artifactId>		
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>		
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
      	<dependency>
   		 <groupId>org.springframework.cloud</groupId>
   		 <artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		</dependencies>
</project>

配置文件:

#端口号
server.port=8085

#热部署静态文件
spring.thymeleaf.cache=false

#应用名称
spring.application.name=springBoot-report-feign

#注册服务器的URL
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/

#请求服务时的超时时间
feign.client.config.feignName.connectTimeout=5000

#读数据时的超时时间
feign.client.config.feignName.readTimeout=5000

#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

js文件:

/**
 * report页面下拉框事件
 */

$(function(){

    $("#selectCityId").change(function () {
        var cityId=$("#selectCityId").val();
        var url='/report/cityId/'+cityId;
        window.location.href=url;
    })

});

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://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"/>

    <title>天气预报</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <h3 th:text="${reportModel.title}">天气</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.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

    <!-- Optional JavaScript -->
    <script type="text/javascript" th:src="@{/js/report.js}"></script>
</body>
</html>

controller层:

package com.csq.study.springboot.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.csq.study.springboot.service.CityClient;
import com.csq.study.springboot.service.WeatherReportService;
import com.csq.study.springboot.vo.City;

@RestController
@RequestMapping("/report")
public class WeatherReportController  {
	    //在应用中添加日志
		private final static Logger logger=LoggerFactory.getLogger(WeatherReportController.class);

		@Autowired
		private WeatherReportService weatherReportService;
		
		@Autowired
		private CityClient cityClient;

		@RequestMapping(value="/cityId/{cityId}", method=RequestMethod.GET)
		//@PathVariable:标识从路径中获取参数
		public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId,Model model) throws Exception {

			List<City> cityList=null;

			try {
				//TODO 改为由城市数据API微服务来提供数据
				cityList= cityClient.listCity();

			} catch (Exception e) {
				logger.error("Exception!",e);
			}

			model.addAttribute("title", "天气预报");
			model.addAttribute("cityId", cityId);
			model.addAttribute("cityList", cityList);
			model.addAttribute("report", weatherReportService.getDataByCityId(cityId));

			return new ModelAndView("report","reportModel",model);
		}

		
}

service接口以及实现类:

CityClient 接口:

package com.csq.study.springboot.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csq.study.springboot.vo.City;



@FeignClient("springBoot-city")
public interface CityClient {

	@RequestMapping(value="/cities", method=RequestMethod.GET)
	List<City> listCity() throws Exception;
}

WeatherDataClient 接口:

package com.csq.study.springboot.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csq.study.springboot.vo.WeatherResponse;



@FeignClient("springBoot-data")
public interface WeatherDataClient {

	@RequestMapping(value="/weather/cityId/{cityId}", method=RequestMethod.GET)
	WeatherResponse getDataByCityId(@PathVariable("cityId") String cityId);
}

WeatherReportService 接口:

package com.csq.study.springboot.service;

import com.csq.study.springboot.vo.Weather;

public interface  WeatherReportService {

	
	Weather getDataByCityId(String cityId);
	
}

 

WeatherReportServiceImpl 实现类

package com.csq.study.springboot.service;

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

import com.csq.study.springboot.vo.Weather;
import com.csq.study.springboot.vo.WeatherResponse;
@Service
public class WeatherReportServiceImpl implements WeatherReportService {

	
	@Autowired
	private WeatherDataClient weatherDataClient;
	
	@Override
	public Weather getDataByCityId(String cityId) {
		//由天气数据API微服务来提供数据
		WeatherResponse resp=weatherDataClient.getDataByCityId(cityId);
		Weather data=resp.getData();
		return data;
	}

}

vo类:依然是之前的City Forecast Weather WeatherResponse Yesterday 五个vo类

启动类:

package com.csq.study.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableDiscoveryClient
@EnableFeignClients
public class Aplication {
	public static void main(String[] args) {
		
		SpringApplication.run(Aplication.class, args);
	}

}

启动该项目之前 需要先启动 redis 、springBoot-City 、springBoot-data 然后启动该项目

访问:http://localhost:8085/report/cityId/101021300

看到下面截图,并且下拉选是可以先选择的,证明项目搭建成功。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值