spring cloud微服务实战 pdf_SpringCloud微服务实战:城市数据API微服务的实现

城市数据API微服务的实现

城市数据API微服务包含了城市数据查询组件。城市数据查询组件提供了城市数据查询的接口。

城市数据由于不会经常被更新,属于静态数据,所以我们已经将ciylst.xml文件放置到resoures目录下,由我们的城市数据服务来读取里面的内容即可。

在micro-weather-report应用的基础上,我们将对其进行逐步的拆分,形成-一个新的微服务msa-weather-city-server应用。

e3b7a96d71773f60be809053d2e45844.png

所需环境

为了演示本例子,需要采用如下开发环境。

JDK 8。

Gradle 4.0。

●Spring Boot Web Starter 2.0.0.M4。

调整服务层代码

在com.waylau.spring.cloud. weather.service包下,我们之前已经定义了该应用的城市数据服务接口CityDataService。

public interface CityDataService {获取城市列表.@return@throws ExceptionList listCity() throws Exception;  CityDataServiceImpl是对CityDataService接口的实现,保留之前的代码即可。package com. waylau . spring. cloud. weather .service;import java. io. BufferedReader;import java. io. InputStreamReader;import java.util.List;import org. springframework.core. io.ClassPathResource;import org. springframework.core. io. Resource;import org. springframework. stereotype. Service;import com. waylau. spring. cloud. weather .util. XmlBuilder;import com. waylau. spring. cloud. weather . vo.City;import com. waylau. spring. cloud. weather.vo.CityList;/**★城市数据服务.* @since 1.0.0 2017年10月23日* @author Way Lau*/@Servicepublic class CityDataService Impl implements CityDataService {@Overridepublic List listCity() throws Exception {//读取XML文件Resource resource = new ClassPathResource ("citylist. xml");Buf feredReader br = new BufferedReader (new InputStreamReader (re-source . getInputStream(), "utf-8")) ;StringBuffer buffer = new StringBuffer() ;String line = "";while ( (line = br. readLine()) != null) {buffer .append(line) ;br.close() ;// XML转为Java对象CityList cityList = (CityList) XmlBuilder . xmlStrToobject (CityList.class, buffer. toString()) ;return cityList. getCityList();}}

除上述CityDataServicelmpl、CityDataService 外,其他服务层的代码都可以删除了。

新增CityController用于返回所有城市的列表。

@RestController@Reques tMapping ("/cities")public class CityController {@Autowiredprivate CityDataService cityDataService;@GetMappingpublic List listCity() throws Exception {return cityDataService.listCity() ;}}

除上述CityController外,其他控制层的代码都可以删除了。

2a954881835a0985af9606634bf23438.png

删除配置类和天气数据同步任务

配置类RestConfiguration、 QuartzConfiguration 及任务类WeatherDataSyncJob 的代码都可以删除了。

清理值对象

清理值对象我们需要保留解析城市相关的类,其他值对象(除City、CityList外)都可以删除了。

清理前端代码、配置及测试用例

已经删除的服务接口的相关测试用例自然也是要一并 删除的。

同时,之前所编写的页面HTML、JS文件也要一并 删除。

最后,要清理Thymeleaf在application.properties文件中的配置,以及build.gradle 文件中的依赖。

Apache HtpClient、Quartz、 Redis 的依赖也一并删除。

保留工具类

工具类在com.waylau.spring.cloud.weather.util包下,之前所创建的XmlBuilder工具类仍然需要保留。

import java. io.Reader;import java. io. StringReader;import javax . xml . bind. JAXBContext;import javax. xml .bind . Unmarshaller;/**XML工具.@since 1.0.0 2017年10月24日@author way Lau*public class XmlBuilder/***将XML字符串转换为指定类型的POJO@param clazz大@param xmlStr@return ★@throws Exception*/public static Object xmlStrTo0bject (Class> clazz, String xmlStr)throws Exception {object xm10bject = null;Reader reader = null;JAXBContext context = JAXBContext. newInstance (clazz) ;//将Xml转成对象的核心接口Unmarshaller unmarshaller = context. createUnmarshaller ();reader = new StringReader (xmlStr) ;xml0bject = unmarshaller . unmarshal (reader) ;if (null!= reader) {reader .close () ;return xmlobject;}}

测试和运行

运行应用,通过访问htp://ocalhost:8080/cities接口来测试。

当接口正常返回数据时,将能看到如图7-5所示的城市接口数据。

cea7d259c3fe5efb504ba66895f5e12b.png

本篇内容给大家讲解的内容是城市数据API微服务的实现

  1. 下篇文章给大家讲解微服务的注册与发现;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值