SpringCloud学习之旅14--weather项目report-eureka-feign-gateway-hystrix(网关、熔断)
目录结构:
build.gradle文件:
// buildscript 代码块中脚本优先执行
buildscript {
// ext 用于定义动态属性
ext {
springBootVersion = '2.0.0.M3'
}
// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
// mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
// 依赖关系
dependencies {
// classpath 声明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
// 指定了生成的编译文件的版本,默认是打成了 jar 包
group = 'com.waylau.spring.cloud'
version = '1.0.0'
// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8
// 使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
//mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
ext {
springCloudVersion = 'Finchley.M2'
}
// 依赖关系
dependencies {
// 该依赖用于编译阶段
compile('org.springframework.boot:spring-boot-starter-web')
// 添加 Spring Boot Thymeleaf Starter 的依赖
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
// Eureka Client
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
// Feign
//compile('org.springframework.cloud:spring-cloud-starter-openfeign')
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '1.4.4.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-feign', version: '1.0.3.RELEASE'
// Hystrix
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
// 该依赖用于测试阶段
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.properties文件:
# 热部署静态文件
spring.thymeleaf.cache=false
spring.application.name: msa-weather-report-eureka-feign-gateway-hystrix
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
feign.client.config.feignName.connectTimeout: 5000
feign.client.config.feignName.readTimeout: 5000
feign.hystrix.enabled=true
js和页面和vo痛前面一样。
package com.waylau.spring.cloud.weather.service;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.waylau.spring.cloud.weather.vo.City;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
* Data Client.
*
* @since 1.0.0 2017年11月28日
* @author <a href="https://waylau.com">Way Lau</a>
*/
@FeignClient(name="msa-weather-eureka-client-zuul", fallback=DataClientFallback.class)
public interface DataClient {
/**
* 获取城市列表
* @return
* @throws Exception
*/
@GetMapping("/city/cities")
List<City> listCity() throws Exception;
/**
* 根据城市ID查询天气数据
*/
@GetMapping("/data/weather/cityId/{cityId}")
WeatherResponse getDataByCityId(@PathVariable("cityId") String cityId);
}
/**
*
*/
package com.waylau.spring.cloud.weather.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.waylau.spring.cloud.weather.vo.City;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
* DataClient Fallback.
*
* @since 1.0.0 2017年12月2日
* @author <a href="https://waylau.com">Way Lau</a>
*/
@Component
public class DataClientFallback implements DataClient {
@Override
public List<City> listCity() throws Exception {
List<City> cityList = null;
cityList = new ArrayList<>();
City city = new City();
city.setCityId("101280601");
city.setCityName("深圳");
cityList.add(city);
city = new City();
city.setCityId("101280301");
city.setCityName("惠州");
cityList.add(city);
return cityList;
}
@Override
public WeatherResponse getDataByCityId(String cityId) {
return null;
}
}
package com.waylau.spring.cloud.weather.service;
import com.waylau.spring.cloud.weather.vo.Weather;
/**
* Weather Report Service.
*
* @since 1.0.0 2017年11月24日
* @author <a href="https://waylau.com">Way Lau</a>
*/
public interface WeatherReportService {
/**
* 根据城市ID查询天气信息
* @param cityId
* @return
*/
Weather getDataByCityId(String cityId);
}
package com.waylau.spring.cloud.weather.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.waylau.spring.cloud.weather.vo.Weather;
import com.waylau.spring.cloud.weather.vo.WeatherResponse;
/**
* Weather Report Service.
*
* @since 1.0.0 2017年11月26日
* @author <a href="https://waylau.com">Way Lau</a>
*/
@Service
public class WeatherReportServiceImpl implements WeatherReportService {
@Autowired
private DataClient dataClient;
@Override
public Weather getDataByCityId(String cityId) {
// 由天气数据API微服务来提供
WeatherResponse resp = dataClient.getDataByCityId(cityId);
Weather data = null;
if (resp != null ) {
return resp.getData();
}
return data;
}
}
package com.waylau.spring.cloud.weather.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.waylau.spring.cloud.weather.service.DataClient;
import com.waylau.spring.cloud.weather.service.WeatherReportService;
import com.waylau.spring.cloud.weather.vo.City;
/**
* Weather Report Controller.
*
* @since 1.0.0 2017年11月24日
* @author <a href="https://waylau.com">Way Lau</a>
*/
@RestController
@RequestMapping("/report")
public class WeatherReportController {
private final static Logger logger = LoggerFactory.getLogger(WeatherReportController.class);
@Autowired
private WeatherReportService weatherReportService;
@Autowired
private DataClient dataClient;
@GetMapping("/cityId/{cityId}")
public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception {
// 获取城市ID列表
List<City> cityList = null;
try {
// 由城市数据API微服务提供数据
cityList = dataClient.listCity();
} catch (Exception e) {
logger.error("Exception!", e);
}
model.addAttribute("title", "老卫的天气预报");
model.addAttribute("cityId", cityId);
model.addAttribute("cityList", cityList);
model.addAttribute("report", weatherReportService.getDataByCityId(cityId));
return new ModelAndView("weather/report", "reportModel", model);
}
}
启动顺序03、08、09、10、13、14博文。
访问链接:http://localhost:8080/report/cityId/101280301
熔断就是一个服务出问题了,不会影响后边的服务,不会影响整个系统,会及时给出相应的提示信息。