照例附上 项目github链接
本项目实现的是将一个简单的天气预报系统一步一步改造成一个SpringCloud微服务系统的过程,本节主要讲的是单块架构改造成微服务架构的过程,最终将原来单块架构的天气预报服务拆分为四个微服务:城市数据API微服务,天气数据采集微服务,天气数据API微服务,天气预报微服务。
本章主要讲解 天气数据API微服务 的实现。
对原来单块架构的天气预报服务进行改进,去除多余的依赖,最终的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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.demo</groupId> <artifactId>sifoudemo02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sifoudemo02</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> <!-- <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> --> </plugin> </plugins> </build> </project>
在service中保留如下接口:
(1)根据城市Id查询天气的接口getDataByCityId
(2)根据城市名称查询天气的接口getDataByCityName
注意:原来我们的天气数据是先从缓存中获取的,若查询不到则调用第三方接口获取天气信息,并将其保存到缓存中。
在我们拆分成微服务架构之后调用第三方接口的行为由天气数据采集微服务中的定时任务进行。
因此在天气数据API微服务中我们的天气数据直接从缓存中进行获取,若在缓存中获取不到对应城市的数据,则直接抛出错误。
@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; } }
在controller中提供根据城市Id和名称获取天气数据的接口。
@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); } }