天氣數據採集微服務 | 從0開始構建SpringCloud微服務(6)

照例附上項目github連接java

本項目實現的是將一個簡單的天氣預報系統一步一步改形成一個SpringCloud微服務系統的過程,本節主要講的是單塊架構改形成微服務架構的過程,最終將原來單塊架構的天氣預報服務拆分爲四個微服務:城市數據API微服務,天氣數據採集微服務,天氣數據API微服務,天氣預報微服務。git

本章主要講解天氣數據採集微服務的實現。github



各微服務的主要功能

在這裏插入圖片描述



天氣數據採集微服務的實現

配置pom文件

對原來單塊架構的天氣預報服務進行改進,去除多餘的依賴,最終的pom文件以下:web

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>sifoudemo02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sifoudemo02</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
     
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.7</version>
        </dependency>    
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        
        <dependency>
              <groupId>org.apache.httpcomponents</groupId>
              <artifactId>httpclient</artifactId>
              <version>4.5.7</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
                <!-- <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions> -->
            </plugin>
        </plugins>
    </build> 


</project>



提供接口

在Service中保留根據城市的Id同步天氣數據的接口。實現方式爲經過城市Id調用第三方接口獲取對應天氣數據,並將其同步到Redis緩存中。redis

@Service
public class WeatherDataCollectionServiceImpl implements WeatherDataCollectionService{
    private static final String WEATHER_URL = "http://wthrcdn.etouch.cn/weather_mini?";

    //設置緩存無效的時間
    private static final long TIME_OUT = 1800L; // 1800s
    
    //httpClient的客戶端
    @Autowired
    private RestTemplate restTemplate;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    //根據城市的Id同步天氣數據
    @Override
    public void syncDataByCityId(String cityId) {
        String url = WEATHER_URL + "citykey=" + cityId;
        this.saveWeatherData(url);
    }
    
    //把天氣數據放在緩存中
    private void saveWeatherData(String url) {
        //將rul做爲天氣數據的key進行保存
        String key=url;
        String strBody=null;
        ValueOperations<String, String>ops=stringRedisTemplate.opsForValue();
        
        //調用服務接口來獲取數據
        ResponseEntity<String>respString=restTemplate.getForEntity(url, String.class);
    
        //判斷請求狀態
        if(respString.getStatusCodeValue()==200) {
            strBody=respString.getBody();
        }
        ops.set(key, strBody,TIME_OUT,TimeUnit.SECONDS);
    }
    
}



定時任務

保留根據城市列表同步所有天氣數據的定時任務,這裏的城市列表後期會經過調用城市數據API微服務獲得。spring

//同步天氣數據
public class WeatherDataSyncJob extends QuartzJobBean{
    private final static Logger logger = LoggerFactory.getLogger(WeatherDataSyncJob.class);

    @Autowired
    private WeatherDataCollectionService weatherDataCollectionService;
    
    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        logger.info("Weather Data Sync Job. Start!");
        //城市ID列表
        //TODO 改成由城市數據API微服務來提供城市列表的數據
        List<City>cityList=null;
        
        try {
            //TODO 改成由城市數據API微服務來提供城市列表的數據
            //獲取xml中的城市ID列表
            //cityList=cityDataService.listCity();
            cityList=new ArrayList<>();
            City city=new City();
            city.setCityId("101280601");
            cityList.add(city);
            
        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("Exception!", e);
        }
        
        //遍歷全部城市ID獲取天氣
        for(City city:cityList) {
            String cityId=city.getCityId();
            logger.info("Weather Data Sync Job, cityId:" + cityId);
            //實現根據cityid定時同步天氣數據到緩存中
            weatherDataCollectionService.syncDataByCityId(cityId);
        }
        logger.info("Weather Data Sync Job. End!");
    }  
    
    
}
相關文章
相關標籤/搜索