通常情况下,我们的业务服务不能为单点服务,否则单个微服务挂掉的话不仅影响当前的服务,对其他的服务也会产生影响,所以本节我们把支付服务也进行集群化,新创建一个支付服务cloud-payment-8002
,创建完以后,项目结构是这样的
按照cloud-payment-8001
的逻辑,完成cloud-payment-8002
支付模块的配置和接口,其application.yml
的eureka
配置如下(记得修改cloud-payment-8002
的端口为 8082)
server:
port: 8002
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7000.com:7000/eureka,http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8002
prefer-ip-address: true
复制代码
cloud-payment-8001
的application.yml
中添加eureka
的配置如下
server:
port: 8001
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7000.com:7000/eureka,http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
复制代码
修改完以后,启动cloud-payment-8002
,查看是否两个支付服务都已经注册成功了
为了区分cloud-payment-8001
和cloud-payment-8002
,我们在返回的数据中添加对应服务信息,修改cloud-payment-8001
中的CPaymentServiceImpl
package com.felix.payment.service.serviceImpl;
import com.felix.common.result.CommonResult;
import com.felix.mbg.mapper.CPaymentMapper;
import com.felix.mbg.model.CPayment;
import com.felix.payment.service.CPaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class CPaymentServiceImpl implements CPaymentService {
@Resource
private CPaymentMapper paymentMapper;
public CommonResult createPayment(String serialNo) {
CPayment payment = new CPayment();
payment.setSerialNo(serialNo);
int result = paymentMapper.insert(payment);
if (result <= 0){
return CommonResult.failed();
}
return new CommonResult(200,"操作成功,当前服务是:payment8081","success",payment);
}
public CommonResult getPayment(Long id) {
CPayment payment = paymentMapper.selectByPrimaryKey(id);
if (payment == null){
return CommonResult.failed();
}
return new CommonResult(200,"操作成功,当前服务是:payment8081","success",payment);
}
}
复制代码
修改cloud-payment-8002
中的CPaymentServiceImpl
package com.felix.payment.service.serviceImpl;
import com.felix.common.result.CommonResult;
import com.felix.mbg.mapper.CPaymentMapper;
import com.felix.mbg.model.CPayment;
import com.felix.payment.service.CPaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class CPaymentServiceImpl implements CPaymentService {
@Resource
private CPaymentMapper paymentMapper;
public CommonResult createPayment(String serialNo) {
CPayment payment = new CPayment();
payment.setSerialNo(serialNo);
int result = paymentMapper.insert(payment);
if (result <= 0){
return CommonResult.failed();
}
return new CommonResult(200,"操作成功,当前服务是:payment8082","success",payment);
}
public CommonResult getPayment(Long id) {
CPayment payment = paymentMapper.selectByPrimaryKey(id);
if (payment == null){
return CommonResult.failed();
}
return new CommonResult(200,"操作成功,当前服务是:payment8082","success",payment);
}
}
复制代码
分别测试一下cloud-payment-8001
再测试一下cloud-payment-8002
两个支付服务都提供相同的业务数据,只是返回的msg
有所不同。
这个时候回想一下,我们在订单服务中调用支付服务的URL
是http://localhost:8001,而当我们有了支付服务的集群后,就需要采用另一个URL
了,修改一下cloud-order-8000
下的OrderController
//private static final String URL_PAYMENT = "http://localhost:8001";
private static final String URL_PAYMENT = "http://CLOUD-PAYMENT";
复制代码
修改ApplicationContextConfig
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
};
}
复制代码
然后,多次访问localhost:8000/order/payment/1,可以发现,数据是从两个服务器上轮流返回的
本节我们完成了支付服务的集群,并且可以进行负载均衡。初步看起来一切都很顺利,然而Eureka
现在已经不再进行更新了,再使用它可能日后会面临其他问题,为什么会带大家走一遍这个流程,因为大部分的分布式平台原理都差不多少,所以,接下来,我们更换服务注册发现中心,使用阿里巴巴的Nacos
.
评论