SpringCloud学习之旅10--weather项目data-eureka

目录结构:


该文就是通过上两篇博文采集到的city信息和存储到Redis中的天气信息,来获取城市的天气信息。

build.gradle文件:

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


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


// 使用了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/" }
}


ext {
springCloudVersion = 'Finchley.M2'
}


// 依赖关系
dependencies {


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


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


// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')


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


dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}

}





application.properties文件:

spring.application.name: msa-weather-data-eureka


eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/


server.port=8083



vo对象05博文里面有,这里就不赘述了。


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


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


/**
 * Weather Data Service.
 * 
 * @since 1.0.0 2017年11月26日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
public interface WeatherDataService {
/**
* 根据城市ID查询天气数据

* @param cityId
* @return
*/
WeatherResponse getDataByCityId(String cityId);


/**
* 根据城市名称查询天气数据

* @param cityId
* @return
*/
WeatherResponse getDataByCityName(String cityName);

}





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


import java.io.IOException;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;


/**
 * WeatherDataService 实现.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
private final static Logger logger = LoggerFactory.getLogger(WeatherDataServiceImpl.class);  

private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Override
public WeatherResponse getDataByCityId(String cityId) {
String uri = WEATHER_URI + "citykey=" + cityId;
return this.doGetWeahter(uri);
}


@Override
public WeatherResponse getDataByCityName(String cityName) {
String uri = WEATHER_URI + "city=" + cityName;
return this.doGetWeahter(uri);
}

private WeatherResponse doGetWeahter(String uri) {
String key = uri;
String strBody = null;
ObjectMapper mapper = new ObjectMapper();
WeatherResponse resp = null;
ValueOperations<String, String>  ops = stringRedisTemplate.opsForValue();
// 先查缓存,缓存有的取缓存中的数据
if (stringRedisTemplate.hasKey(key)) {
logger.info("Redis has data");
strBody = ops.get(key);
} else {
logger.info("Redis don't has data");
// 缓存没有,抛出异常
throw new RuntimeException("Don't has data!");
}


try {
resp = mapper.readValue(strBody, WeatherResponse.class);
} catch (IOException e) {
//e.printStackTrace();
logger.error("Error!",e);
}

return resp;
}


}






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


import org.springframework.beans.factory.annotation.Autowired;
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 com.waylau.spring.cloud.weather.service.WeatherDataService;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
 * Weather Controller.
 * 
 * @since 1.0.0 2017年11月22日
 * @author <a href="https://waylau.com">Way Lau</a> 
 */
@RestController
@RequestMapping("/weather")
public class WeatherController {
@Autowired
private WeatherDataService weatherDataService;

@GetMapping("/cityId/{cityId}")
public WeatherResponse getWeatherByCityId(@PathVariable("cityId") String cityId) {
return weatherDataService.getDataByCityId(cityId);
}

@GetMapping("/cityName/{cityName}")
public WeatherResponse getWeatherByCityName(@PathVariable("cityName") String cityName) {
return weatherDataService.getDataByCityName(cityName);
}

}




package com.waylau.spring.cloud.weather;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient
public class Application {


public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}



这里需要另外启动03和08和09三个项目。


访问链接:http://localhost:8083/weather/cityId/101280101


这里是先看Redis缓存中有没有数据,如果没有,则在重新调用接口获取数据。



//因为是根据cityId同步保存的数据

访问链接:http://localhost:8083/weather/cityName/广州


这里就会报出我们定义的运行时异常,没有数据。



猜你喜欢

转载自blog.csdn.net/qq_33371766/article/details/80660477
今日推荐