Eureka集群
- 高可用、故障容错
- 负载均衡
- 节点之间相互注册,相互守望
cloud-eureka-server7001:Eureka集群
application-7001.yml
代码语言:javascript复制server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://eureka7002.com:7002/eureka/
application-7002.yml
代码语言:javascript复制server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://eureka7001.com:7001/eureka/
cloud-provider-payment8001:服务提供者集群
application.yml
代码语言:javascript复制eureka:
client:
#表示是否将自己注册进Eurekaserver默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
application-8001.yml
代码语言:javascript复制server:
port: 8001
application-8002.yml
代码语言:javascript复制server:
port: 8002
PaymentController
代码语言:javascript复制@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@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, "插入数据库成功,serverPort: " serverPort, result);
} else {
return new CommonResult(444, "插入数据库失败", null);
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
if (payment != null) {
return new CommonResult(200, "查询成功,serverPort: " serverPort, payment);
} else {
return new CommonResult(444, "没有对应记录,查询ID: " id, null);
}
}
}
cloud-consumer-order1080:消费者
application.yml
代码语言:javascript复制server:
port: 1080
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进Eurekaserver默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
info:
app:
name: ${spring.application.name}
company:
name: @project.groupId@
build:
artifactId: @project.artifactId@
version: @project.version@
开启负载均衡
代码语言:javascript复制@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- 使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
- Eureka依赖里包含了Ribbon,因此具备负载均衡的能力
请求地址修改
代码语言:javascript复制@Slf4j
@RestController
public class OrderController {
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@PostMapping("/consumer/payment/create")
public CommonResult<Payment> create(@RequestBody Payment payment){
return restTemplate.postForObject(PAYMENT_URL "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL "/payment/get/" id, CommonResult.class);
}
}
- PAYMENT_URL修改为服务提供者的application.name,通过Eureka转换为真实地址
访问注册中心
Eureka Server的一个节点:http://eureka7002.com:7002/
- Eureka Server的另一个节点
- 注册上的Eureka Client
测试
GET http://127.0.0.1:1080/consumer/payment/get/10
第一次
代码语言:javascript复制{
"code": 200,
"message": "查询成功,serverPort: 8002",
"data": {
"id": 10,
"serial": null
}
}
第二次
代码语言:javascript复制{
"code": 200,
"message": "查询成功,serverPort: 8001",
"data": {
"id": 10,
"serial": null
}
}
- 通过返回的端口号得知,两次请求的服务提供者是不同的,已达到负载均衡的目的
Post Views: 166