首页 文章详情

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

愿天堂没有BUG | 92 2022-05-27 04:03 0 0 0
UniSMS (合一短信)

城市数据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。

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用于返回所有城市的列表。

@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工具类仍然需要保留。

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给大家分享的内容,大家有收获的话可以分享下,想学习更多的话可以到微信公众号里找我,我等你哦。

good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter