为了尽量模拟多台服务器的情况,我们现在把之前的eureka
模块复制一份出来,粘贴到mall
下,现在它在项目中显示的仅仅是个文件夹
我们把它改造成一个新的
服务中心
,点击Project Structure
,在弹出的窗口中点击+
,选择Import Module
然后在新的窗口中选择刚才复制粘贴出来的
eureka01
,点击open
一路点击
下一步
,完成之后,修改Name
为eureka01
然后,为新建的
eureka01
创建一个启动配置,打开Eidt Configurations
,点击+
,选择Spring Boot
配置名称为
eureka01
,Main class
为com.felix.EurekaApplication
,Use classpath of module
选择eureka01
,如图好了,现在我们已经有了两台
服务中心
服务器了,按照上面的方法再创建一台,命名为eureka02
(图中可以看到我在此之前已经复制了一份新的goods01
,并创建了模块,不再是之前的仅仅修改配置了,也是为了测试方便)现在,我们把三台
服务中心
分别进行如下配置:
eureka
server: port: 7000 spring: application: name: eureka-service security: user: name: felix password: 123456 eureka: client: register-with-eureka: true #是否注册到Eureka服务中 fetch-registry: true #是否从Eureka服务中获取注册信息 service-url: #Eureka客户端与服务端进行交互的地址 defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7001/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7002/eureka/
eureka01
server: port: 7001 spring: application: name: eureka-service security: user: name: felix password: 123456 eureka: client: register-with-eureka: true #是否注册到Eureka服务中 fetch-registry: true #是否从Eureka服务中获取注册信息 service-url: #Eureka客户端与服务端进行交互的地址 defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7000/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7002/eureka/
eureka02
server: port: 7002 spring: application: name: eureka-service security: user: name: felix password: 123456 eureka: client: register-with-eureka: true #是否注册到Eureka服务中 fetch-registry: true #是否从Eureka服务中获取注册信息 service-url: #Eureka客户端与服务端进行交互的地址 defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7000/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:7001/eureka/
这样,一个高可用的服务中心
集群就已经搭建好了,我们访问其中一台服务中心
查看http://127.0.0.1:7000/
目前,我们已经打开了6台服务器,分别为
订单服务器
1台
商品服务器
2台
服务中心服务器
3台
对于订单服务
来讲,即使2台商品服务器
,3台服务中心服务器
全部Down机,仍然会提供服务,但是服务中存在异常。而如果是1台商品服务器
、2台服务中心服务器
Down机,我们的业务仍然是可以正常进行的,下面我们来验证一下,首先关闭1台商品服务器
、2台服务中心服务器
现在访问
订单服务
的接口http://127.0.0.1:8100/order/3
服务器对外并未表现出明显异常,我们再关掉最后的1台
商品服务器
、1台服务中心服务器
再次访问
订单服务
的接口http://127.0.0.1:8100/order/3
纳尼,和预料的不一样啊。。。其实还是正常的,这个时候只是因为在获取商品信息的时候返回的数据为空,而我们没有捕获异常,这里,我们在
OrderService
中的findOrderById
捕获异常
public Order findOrderById(Long id){
Order order = order_map.get(id);
for (OrderDetails orderDetails : order.getOrderDetailsList()){
try {
Goods goods = goodsService.findGoodsById(orderDetails.getGoods().getGoodsid());
orderDetails.setGoods(goods);
}catch (Exception e){
e.printStackTrace();
}
}
return order;
}
再次模拟刚才的情况,访问http://127.0.0.1:8100/order/3
好了,这次的
订单服务器
看起来算是正常了,商品信息为空是因为商品服务器
彻底Down机了。
现在我们的高可用服务中心已经搭建完毕,按照现在的逻辑,理论上6台服务器,即使5台都Down机了,系统仍然能够正常提供服务。
以上内容转载请注明出处,同时也请大家不吝你的关注和下面的赞赏
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
网友评论