集群原理
微服务RPC远程服务调用最核心的是什么?
高可用,如果服务注册中心里只有一台机器,如果挂了那就无法访问,所以需要配置多台机器来解决这个问题,一台机器挂了还有另外的机器可以访问。
搭建注册中心集群,实现负载均衡+故障容错
- 参考cloud-eureka-server7001搭建cloud-eureka-server7002
- 修改pom
- 修改yml
- 单机改为集群服务名不能相同需要修改hostname以及注册地址 (多台机器需要相互注册)
- 7002配置
- 单机改为集群服务名不能相同需要修改hostname以及注册地址 (多台机器需要相互注册)
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
- 7001配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
- 配置主启动
- 省略。。。
- 将支付服务8001微服务发布到上面2台Eureka集群配置中
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
- 将订单服务80微服务发布到上面2台Eureka集群配置中
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
- 测试启动
- 支付服务提供者8001集群环境构建
- 建立cloud-provider-payment8002
- pom
- yml
- 主启动
- 业务类
- 修改8001/8002 conrtoller
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @auther zzyy
* @create 2020-01-27 21:17
*/
@RestController
@Slf4j
public class PaymentController
{
@Value("${server.port}")
private String serverPort;
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment)
{
int result = paymentService.create(payment);
log.info("*****插入操作返回结果:" + result);
if(result > 0)
{
return new CommonResult(200,"插入成功,返回结果"+result+"\t 服务端口:"+serverPort,payment);
}else{
return new CommonResult(444,"插入失败",null);
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
{
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:{}",payment);
if (payment != null) {
return new CommonResult(200,"查询成功"+"\t 服务端口:"+serverPort,payment);
}else{
return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
}
}
}
-
配置负载均衡
- 服务地址不能写死,改为注册中心的服务注册名
重新启动有报错,找不到地址
需要配置restRemplate添加@LoadBalanced注册实现负载均衡
测试启动 发现端口号是在轮询的,成功