bug注册和发现
常用的注册中心:
EurekaNacosZookeeperConsulETCD.... 搭建Eureka注册中心:
- 新建 spring 模块:sp05-eureka添加依赖: eureka-server pom.xml
springcloud1 cn.tedu 1.0-SNAPSHOT 4.0.0 sp05-eureka0.0.1-SNAPSHOT sp05-eureka Demo project for Spring Boot 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-serverorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
3.application.yml
spring:
application:
name: eureka
# eureka默认端口8761
server:
port: 2001
eureka:
server:
enable-self-preservation: false # 禁用自我保护模式
instance:
hostname: eureka1 # 主机名
client: #针对单台服务器
register-with-eureka: false #不向自己注册
fetch-registry: false #不向自己拉取
4.启动类注解
访问
四条运行机制:- 注册 客户端连接服务器进行注册,会一次一次反复注册,直到注册成功为止拉取 客户端每30秒拉取一次注册表,刷新本地缓存的注册表心跳 客户端每30秒发送一次心跳 服务器连续3次收不到一个服务的心跳,会删除它的注册信息自我保护模式 由于网络中断,大量服务器出现心跳异常 15分钟内,85%服务器出现心跳异常,会进入自我保护模式, 所有的注册信息不删除,等待网络恢复 开发调试期间,可以禁用保护模式,避免影响测试
1.修改host文件
win + r,输入 drivers
在 etc 文件夹找到 hosts 文件
用管理员打编辑器,修改 hosts 文件
添加:
127.0.0.1 eureka1 127.0.0.1 eureka2
2.修改pom.xml->2/3/4模块
alt+insert
3.修改yml文件->2/3/4模块
4.启动2/3/4模块刷新网页
bug ->启动2没启动5 商品高可用- 运行配置的下拉菜单,选择 Edit Configration找到 02 的启动配置修改配置名称: Sp02Item-8001Program arguments: --server.port=8001左侧点 copy configration,复制出来修改成 8002
测试 Eureka高可用
- 新建两个 profile 配置:
application-eureka1.ymlapplication-eureka2.yml启动配置:
- 名称: Sp05Eureka-2001Program argument: --spring.profiles.active=eureka1点复制名称 2002,激活 eureka2
修改application.yml->2/3/4模块
04远程调用02和031.04添加依赖 feign
2.启动类添加注解
3.远程调用接口
ItemClient
package cn.tedu.sp04.feign;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.web.util.JsonResult;
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 java.util.List;
//item-service---------localhost:8001/localhost:8002
@FeignClient("item-service")
public interface ItemClient {
@GetMapping("/{orderId}")
JsonResult> getItems(@PathVariable("orderId") String orderId);
@PostMapping("/decreaseNumber")
JsonResult> decreaseNumber(@RequestBody List- items);
}
UserClient
package cn.tedu.sp04.feign;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
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.RequestParam;
@FeignClient("user-service")
public interface UserClient {
@GetMapping("/{userId}")
JsonResult getUser(@PathVariable("userId") Integer userId);
@PostMapping("/{userId}/score")
JsonResult> addScore(@PathVariable("userId") Integer userId,
@RequestParam("score") Integer score);
}
4.完成 OrderServiceImpl 远程调用
package cn.tedu.sp04.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.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private ItemClient itemClient;
@Autowired
private UserClient userClient;
@Override
public Order getOrder(String orderId) {
log.info("获取订单,orderId="+orderId);
// TODO: 远程调用商品,获取商品列表
JsonResult> items = itemClient.getItems(orderId);
// TODO: 远程调用用户,获取用户数据
// 真实项目中应该使用应登录用户的id
JsonResult user = userClient.getUser(8);
Order order = new Order();
order.setId(orderId);
// order.setUser(用户);
order.setUser(user.getData());
// order.setItems(商品);
order.setItems(items.getData());
return order;
}
@Override
public void create(Order order) {
log.info("创建订单: "+order);
// TODO: 远程调用商品,减库存
itemClient.decreaseNumber(order.getItems());
// TODO: 远程调用用户,加积分
userClient.addScore(order.getUser().getId(), 1000);
}
}
5.重启2、4测试
Zuul API网关统一的调用入口统一的权限校验集成 Ribbon集成 Hystrix
统一的调用入口:
新建 spring 模块: sp06-zuul
pom.xml
zuuleureka clientsp01-commons
yml
# ** 包含深层的子路径
# * 只包含一层路径
# 下面的配置是默认配置,zuul会根据注册表的注册信息,来自动完成配置
# 最好手动配置,防止注册表不全
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/** 启动类添加注解: @EnableZuulProxy
统一的权限校验:
http://localhost:3001/item-service/y45tr3432r 没有登录不允许访问
http://localhost:3001/item-service/y45tr3432r?token=7yu4t53r 已登录可以访问
- 新建过滤器类 AccessFilter,继承 ZuulFilter按规则实现添加 @Component
zuul的自动配置类,会自动发现过滤器实例,完成自动配置



