Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ed8498

Browse files
committed
update
1 parent2f3837a commit6ed8498

File tree

5 files changed

+66
-55
lines changed

5 files changed

+66
-55
lines changed

‎SUMMARY.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*[微服务架构概述](docs/msa-boot/msa-boot-overview.md)
1010
*[微服务 API 设计原则](docs/msa-boot/msa-boot-api.md)
1111
*[微服务存储设计原则](docs/msa-boot/msa-boot-store.md)
12-
*[第一个微服务实现——天气预报服务](docs/msa-boot/first-api.md) (完)
12+
*[第一个微服务实现——天气预报服务](https://waylau.com/spring-boot-weather-report/) (完)
1313
*[微服务的注册与发现](docs/register-discover/register-discover.md)
1414
*[服务发现的意义](docs/register-discover/discover-meaning.md)
1515
*[如何集成Eureka](docs/register-discover/eureka.md)

‎docs/msa-boot/first-api.md‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#第一个微服务实现——天气预报服务
22

33

4-
本章节,我们将基于 Spring Boot 技术来实现我们的第一个微服务天气预报应用——micro-weather。micro-weather 的作用是实现天气预报功能,可以根据不同的城市,查询该城市的实时天气情况。
4+
本章节,我们将基于 Spring Boot 技术来实现我们的第一个微服务天气预报应用——micro-weather-basic。micro-weather-basic 的作用是实现简单的天气预报功能,可以根据不同的城市,查询该城市的实时天气情况。
55

66
##开发环境
77

@@ -13,15 +13,12 @@
1313

1414
理论上,天气的数据是天气预报的实现基础。本应用与实际的天气数据无关,理论上,可以兼容多种数据来源。但为求简单,我们在网上找了一个免费、可用的天气数据接口。
1515

16-
* 天气数据来源为中华万年历。
16+
* 天气数据来源为中华万年历。例如:
17+
* 通过城市名字获得天气数据 :<http://wthrcdn.etouch.cn/weather_mini?city=深圳>
18+
* 通过城市id获得天气数据:<http://wthrcdn.etouch.cn/weather_mini?citykey=101280601>
1719
* 城市ID列表。每个城市都有一个唯一的ID作为标识。见<http://cj.weather.com.cn/support/Detail.aspx?id=51837fba1b35fe0f8411b6df> 或者<http://mobile.weather.com.cn/js/citylist.xml>
1820

19-
天气调用,接口示例,我们以“深圳”城市为例,访问:
20-
21-
* 通过城市名字获得天气数据 :<http://wthrcdn.etouch.cn/weather_mini?city=深圳>
22-
* 通过城市id获得天气数据:<http://wthrcdn.etouch.cn/weather_mini?citykey=101280601>
23-
24-
可用看到如下天气数据返回。
21+
调用天气服务接口示例,我们以“深圳”城市为例,可用看到如下天气数据返回。
2522

2623
```json
2724
{
@@ -128,10 +125,10 @@ dependencies {
128125
//...
129126
}
130127
```
131-
##创建天气信息实体 WeatherInfo
128+
##创建天气信息相关的值对象
132129

133130

134-
创建`com.waylau.spring.cloud.vo`包,用于相关值对象。创建天气信息实体 Weather
131+
创建`com.waylau.spring.cloud.vo`包,用于相关值对象。创建天气信息类 Weather
135132

136133

137134
```java
@@ -329,33 +326,39 @@ WeatherResponse 作为整个消息的返回对象
329326

330327
```java
331328
publicclassWeatherResponseimplementsSerializable {
332-
333-
privatestaticfinallong serialVersionUID=1L;
334-
335-
privateWeather data;// 消息数据
336-
privateString status;// 消息状态
337-
privateString desc;// 消息描述
338-
329+
330+
privatestaticfinallong serialVersionUID=1L;
331+
332+
privateWeather data;// 消息数据
333+
privateString status;// 消息状态
334+
privateString desc;// 消息描述
335+
339336
publicWeathergetData() {
340337
return data;
341338
}
339+
342340
publicvoidsetData(Weatherdata) {
343341
this.data= data;
344342
}
343+
345344
publicStringgetStatus() {
346345
return status;
347346
}
347+
348348
publicvoidsetStatus(Stringstatus) {
349349
this.status= status;
350350
}
351+
351352
publicStringgetDesc() {
352353
return desc;
353354
}
355+
354356
publicvoidsetDesc(Stringdesc) {
355357
this.desc= desc;
356358
}
357-
359+
358360
}
361+
359362
```
360363

361364
##服务接口及实现
@@ -386,12 +389,12 @@ public interface WeatherDataService {
386389
```java
387390
@Service
388391
publicclassWeatherDataServiceImplimplementsWeatherDataService {
389-
390-
@Autowired
391-
privateRestTemplate restTemplate;
392392

393-
privatefinalStringWEATHER_API="http://wthrcdn.etouch.cn/weather_mini";
394-
393+
@Autowired
394+
privateRestTemplate restTemplate;
395+
396+
privatefinalStringWEATHER_API="http://wthrcdn.etouch.cn/weather_mini";
397+
395398
@Override
396399
publicWeatherResponsegetDataByCityId(StringcityId) {
397400
String uri=WEATHER_API+"?citykey="+ cityId;
@@ -400,27 +403,27 @@ public class WeatherDataServiceImpl implements WeatherDataService {
400403

401404
@Override
402405
publicWeatherResponsegetDataByCityName(StringcityName) {
403-
String uri=WEATHER_API+"?city="+ cityName;
406+
String uri=WEATHER_API+"?city="+ cityName;
404407
returnthis.doGetWeatherData(uri);
405408
}
406-
409+
407410
privateWeatherResponsedoGetWeatherData(Stringuri) {
408411
ResponseEntity<String> response= restTemplate.getForEntity(uri,String.class);
409412
String strBody=null;
410-
413+
411414
if (response.getStatusCodeValue()==200) {
412415
strBody= response.getBody();
413416
}
414-
415-
ObjectMapper mapper=newObjectMapper();
417+
418+
ObjectMapper mapper=newObjectMapper();
416419
WeatherResponse weather=null;
417-
420+
418421
try {
419422
weather= mapper.readValue(strBody,WeatherResponse.class);
420423
}catch (IOException e) {
421424
e.printStackTrace();
422-
}
423-
425+
}
426+
424427
return weather;
425428
}
426429

@@ -487,3 +490,6 @@ public class RestConfiguration {
487490

488491
![weather-data](../../images/msa-boot/weather-data.jpg)
489492

493+
##源码
494+
495+
本章节的源码,在`micro-weather-basic`目录下。

‎images/msa-boot/weather-data.jpg‎

412 KB
Loading

‎samples/micro-weather-basic/src/main/java/com/waylau/spring/cloud/service/WeatherDataServiceImpl.java‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
* 天气数据服务.
1818
*
1919
* @since 1.0.0 2017年9月2日
20-
* @author <a href="https://waylau.com">Way Lau</a>
20+
* @author <a href="https://waylau.com">Way Lau</a>
2121
*/
2222
@Service
2323
publicclassWeatherDataServiceImplimplementsWeatherDataService {
24-
25-
@Autowired
26-
privateRestTemplaterestTemplate;
2724

28-
privatefinalStringWEATHER_API="http://wthrcdn.etouch.cn/weather_mini";
29-
25+
@Autowired
26+
privateRestTemplaterestTemplate;
27+
28+
privatefinalStringWEATHER_API ="http://wthrcdn.etouch.cn/weather_mini";
29+
3030
@Override
3131
publicWeatherResponsegetDataByCityId(StringcityId) {
3232
Stringuri =WEATHER_API +"?citykey=" +cityId;
@@ -35,27 +35,27 @@ public WeatherResponse getDataByCityId(String cityId) {
3535

3636
@Override
3737
publicWeatherResponsegetDataByCityName(StringcityName) {
38-
Stringuri =WEATHER_API +"?city=" +cityName;
38+
Stringuri =WEATHER_API +"?city=" +cityName;
3939
returnthis.doGetWeatherData(uri);
4040
}
41-
41+
4242
privateWeatherResponsedoGetWeatherData(Stringuri) {
4343
ResponseEntity<String>response =restTemplate.getForEntity(uri,String.class);
4444
StringstrBody =null;
45-
45+
4646
if (response.getStatusCodeValue() ==200) {
4747
strBody =response.getBody();
4848
}
49-
50-
ObjectMappermapper =newObjectMapper();
49+
50+
ObjectMappermapper =newObjectMapper();
5151
WeatherResponseweather =null;
52-
52+
5353
try {
5454
weather =mapper.readValue(strBody,WeatherResponse.class);
5555
}catch (IOExceptione) {
5656
e.printStackTrace();
57-
}
58-
57+
}
58+
5959
returnweather;
6060
}
6161

‎samples/micro-weather-basic/src/main/java/com/waylau/spring/cloud/vo/WeatherResponse.java‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,38 @@
66
* 返回消息对象.
77
*
88
* @since 1.0.0 2017年9月2日
9-
* @author <a href="https://waylau.com">Way Lau</a>
9+
* @author <a href="https://waylau.com">Way Lau</a>
1010
*/
1111
publicclassWeatherResponseimplementsSerializable {
12-
13-
privatestaticfinallongserialVersionUID =1L;
14-
15-
privateWeatherdata;// 消息数据
16-
privateStringstatus;// 消息状态
17-
privateStringdesc;// 消息描述
18-
12+
13+
privatestaticfinallongserialVersionUID =1L;
14+
15+
privateWeatherdata;// 消息数据
16+
privateStringstatus;// 消息状态
17+
privateStringdesc;// 消息描述
18+
1919
publicWeathergetData() {
2020
returndata;
2121
}
22+
2223
publicvoidsetData(Weatherdata) {
2324
this.data =data;
2425
}
26+
2527
publicStringgetStatus() {
2628
returnstatus;
2729
}
30+
2831
publicvoidsetStatus(Stringstatus) {
2932
this.status =status;
3033
}
34+
3135
publicStringgetDesc() {
3236
returndesc;
3337
}
38+
3439
publicvoidsetDesc(Stringdesc) {
3540
this.desc =desc;
3641
}
37-
42+
3843
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp