慕课网的课程《从天气项目看 Spring Cloud 微服务治理》应该是入门spring cloud微服务最简单的实战视频了。这是学习的第一篇文章,因为业务很简单,就是获取天气并且展示出来,本节说明一下如何获取天气信息。代码详见尾部,本系列所有代码都分类保存再github上了。
一、获取天气信息
数据来源:
http://wthrcdn.etouch.cn/weather_mini?citykey=xxx
或者
http://wthrcdn.etouch.cn/weather_mini?city=xxx
首先我们如何做一个查询天气的接口呢?其实特别简单,就是用HttpClient
这个客户端来调用以上的接口,就可以拿到数据了。
我们所需要做的工作也非常少,就是封装一下数据,请求一下参数即可。
请求的数据是json,我们这里准备好数据bean即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @Data public class WeatherResponse implements Serializable { private Weather data; private Integer status; private String desc; } @Data public class Weather implements Serializable { private String city; private String aqi; private List<Forecast> forecast; private String ganmao; private String wendu; private Yesterday yesterday; } @Data public class Forecast implements Serializable { private String date; private String high; private String fengli; private String low; private String fengxiang; private String type; } @Data public class Yesterday implements Serializable { private String date; private String high; private String fx; private String low; private String fl; private String type; }
ok,数据载体已经好了,下面就是用RestTemplate
调用url
取获取天气信息,用上面的bean来封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 @Service public class WeatherDataServiceImpl implements IWeatherDataService { @Autowired private RestTemplate restTemplate; private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?" ; @Override public WeatherResponse getDataByCityId (String cityId) { String uri = WEATHER_URI + "citykey=" +cityId; return doGetWeather(uri); } @Override public WeatherResponse getDataByCityName (String cityName) { String uri = WEATHER_URI + "city=" +cityName; return doGetWeather(uri); } private WeatherResponse doGetWeather (String uri) { ResponseEntity<String> resString = restTemplate.getForEntity(uri,String.class); ObjectMapper mapper = new ObjectMapper(); WeatherResponse resp = null ; String strBody = null ; if (resString.getStatusCodeValue() == 200 ){ strBody = resString.getBody(); } try { resp = mapper.readValue(strBody,WeatherResponse.class); }catch (IOException e){ e.printStackTrace(); } return resp; } }
最后再用一个controller来给一个接口即可。
二、注意
直接启动项目会报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-11-19 15:19:54.732 ERROR 13924 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field restTemplate in com.swg.weatherbasic.service.impl.WeatherDataServiceImpl required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
我们可以看到提示信息是:'org.springframework.web.client.RestTemplate' that could not be found.
,错误就很明显了,这个玩意根本就没有在spring中注册,怎么可以注入呢?
所以,我们需要向spring注册一下这个bean:
1 2 3 4 5 6 7 8 9 10 @Configuration public class RestConfig { @Autowired private RestTemplateBuilder builder; @Bean public RestTemplate restTemplate () { return builder.build(); } }
三、彩蛋
将此小项目作为一个小版本,直接保存到码云上。如何做呢?
其实很简单,先去码云上新建一个项目。然后在本地某一个文件夹下执行
然后将我们的项目直接拷贝到这个文件夹下。执行
1 2 3 git add . git commit -am 'weather-basic' git push origin master
这样就可以了。
代码地址:spring-cloud-weather-action----01
下面章节的代码依次类推,可能就不再赘述了。本项目的springboot
版本是2.1.0.RELEASE
,spring cloud
用的是Finchley.RELEASE
版本。