01.项目结构如下:
02.修改sp04-orderservice项目,添加feign,调用item service和user service
1.sp04-orderservice项目的pom.xml
添加以下依赖:
actuator
feign
hystrix
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
cn.tedu
sp04-orderservice
0.0.1-SNAPSHOT
sp04-orderservice
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
cn.tedu
sp01-commons
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-maven-plugin
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR12
pom
import
2.sp04-orderservice项目的application.yml
ribbon 重试和 hystrix 超时这里没有设置,采用了默认值
spring:
application:
name: order-service
server:
port: 8201
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: hystrix.stream
3.主程序
package cn.tedu.sp04;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//@EnableDiscoveryClient
//@SpringBootApplication
@SpringCloudApplication
@EnableFeignClients
public class Sp04OrderserviceApplication {
public static void main(String[] args) {
SpringApplication.run(Sp04OrderserviceApplication.class, args);
}
}
4.ItemFeignService
package cn.tedu.sp04.order.feignclient;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.web.util.JsonResult;
@FeignClient(name="item-service", fallback = ItemFeignServiceFB.class)
public interface ItemFeignService {
@GetMapping("/{orderId}")
JsonResult> getItems(@PathVariable String orderId);
@PostMapping("/decreaseNumber")
JsonResult decreaseNumber(@RequestBody List- items);
}
5.UserFeignService
package cn.tedu.sp04.order.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
@FeignClient(name="user-service", fallback = UserFeignServiceFB.class)
public interface UserFeignService {
@GetMapping("/{userId}")
JsonResult getUser(@PathVariable Integer userId);
@GetMapping("/{userId}/score")
JsonResult addScore(@PathVariable Integer userId, @RequestParam Integer score);
}
6.ItemFeignServiceFB
获取商品列表的降级方法,模拟使用缓存数据
package cn.tedu.sp04.order.feignclient;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.web.util.JsonResult;
@Component
public class ItemFeignServiceFB implements ItemFeignService {
@SuppressWarnings("unchecked")
@Override
public JsonResult> getItems(String orderId) {
if(Math.random()<0.5) {
return JsonResult.ok().data(
Arrays.asList(new Item[] {
new Item(1,"缓存手机商品",2),
new Item(2,"缓存电脑商品",1),
new Item(3,"缓存知识的海洋",3),
new Item(4,"缓存奥特曼玩具",1),
new Item(5,"缓存喜羊羊玩具",5)
})
);
}
return JsonResult.err("获取订单商品失败");
}
@Override
public JsonResult decreaseNumber(List- items) {
return JsonResult.err("修改库存失败");
}
}
7.UserFeignServiceFB
获取用户信息的降级方法,模拟使用缓存数据
package cn.tedu.sp04.order.feignclient;
import org.springframework.stereotype.Component;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
@Component
public class UserFeignServiceFB implements UserFeignService {
@Override
public JsonResult getUser(Integer userId) {
if(Math.random()<0.4) {
return JsonResult.ok(new User(userId, "缓存name"+userId, "缓存pwd"+userId));
}
return JsonResult.err("获取用户信息失败");
}
@Override
public JsonResult addScore(Integer userId, Integer score) {
return JsonResult.err("增加用户积分失败");
}
}
8.OrderServiceImpl
package cn.tedu.sp04.order.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.order.feignclient.ItemFeignService;
import cn.tedu.sp04.order.feignclient.UserFeignService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ItemFeignService itemService;
@Autowired
private UserFeignService userService;
@Override
public Order getOrder(String orderId) {
//TODO: 调用user-service获取用户信息
JsonResult user = userService.getUser(7);
//TODO: 调用item-service获取商品信息
JsonResult> items = itemService.getItems(orderId);
Order order = new Order();
order.setId(orderId);
order.setItems(items.getData());
order.setUser(user.getData());
return order;
}
@Override
public void addOrder(Order order) {
//TODO: 调用item-service减少商品库存
itemService.decreaseNumber(order.getItems());
//TODO: 调用user-service增加用户积分
userService.addScore(7, 100);
log.info("保存订单:"+order);
}
}
9.order-service 配置启动参数,启动两台服务器
–server.port=8201
–server.port=8202
10.启动服务,访问测试
根据orderid,获取订单
http://localhost:8201/123abc
http://localhost:8202/123abc
保存订单
http://localhost:8201/
http://localhost:8202/
11.hystrix dashboard 监控 order service 断路器
访问 http://localhost:4001/hystrix ,填入 order service 的断路器监控路径,启动监控
http://localhost:8201/actuator/hystrix.stream
http://localhost:8202/actuator/hystrix.stream