前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud微服务实战:城市数据API微服务的实现

SpringCloud微服务实战:城市数据API微服务的实现

作者头像
愿天堂没有BUG
发布2022-10-28 15:53:04
3550
发布2022-10-28 15:53:04
举报
文章被收录于专栏:愿天堂没有BUG(公众号同名)

城市数据API微服务的实现

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

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

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

所需环境

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

JDK 8。

Gradle 4.0。

●Spring Boot Web Starter 2.0.0.M4。

调整服务层代码

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

代码语言:javascript
复制
public interface CityDataService {
获取城市列表.
@return
@throws Exception
List<city> 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 <a href="https:/ /waylau. com">Way Lau</a>
*/
@Service
public class CityDataService Impl implements CityDataService {
@Override
public List<City> 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 (City
List.class, buffer. toString()) ;
return cityList. getCityList();
}
}

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

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

代码语言:javascript
复制
@RestController
@Reques tMapping ("/cities")
public class CityController {
@Autowired
private CityDataService cityDataService;
@GetMapping
public List<City> listCity() throws Exception {
return cityDataService.listCity() ;
}
}

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

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

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

清理值对象

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

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

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

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

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

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

保留工具类

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

代码语言:javascript
复制
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 <a href="https:/ /waylau. com">way Lau</a>
*
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所示的城市接口数据。

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

  1. 下篇文章给大家讲解微服务的注册与发现;
  2. 觉得文章不错的朋友可以转发此文关注小编;
  3. 感谢大家的支持!

本文就是愿天堂没有BUG给大家分享的内容,大家有收获的话可以分享下,想学习更多的话可以到微信公众号里找我,我等你哦。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 愿天堂没有BUG 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 城市数据API微服务的实现
  • 所需环境
  • 调整服务层代码
  • 删除配置类和天气数据同步任务
  • 清理值对象
  • 清理前端代码、配置及测试用例
  • 保留工具类
  • 测试和运行
  • 本篇内容给大家讲解的内容是城市数据API微服务的实现
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档