SpringCloud-支付模块和消费模块入驻Eureka集群(Day3)

本文介绍了如何在微服务架构中配置Eureka集群,包括支付和消费模块的设置,详细展示了YAML配置文件内容,如Eureka客户端的注册与抓取注册信息设置,以及服务发现的URL。同时,文章提到了消费模块控制层如何通过RestTemplate进行负载均衡调用,并展示了启用@LoadBalanced的配置。虽然当前只有一个支付模块实例,但后续会扩展到多个实例以展示负载均衡效果。
摘要由CSDN通过智能技术生成

支付和消费模块接入Euraka集群

支付模块的设置

更改支付模块的yml

server:
  port: 8001

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka,http://localhost:7002/eureka,http://192.168.0.135:7003/eureka

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.gcl.springcloud.entities

消费模块的设置

更改消费模块的yml

server:
  port: 8080

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka,http://localhost:7002/eureka,http://192.168.0.135:7003/eureka

更改消费模块的控制层调用(主要是请求的地址更改了,更改为提供服务的微服务应用名)

package com.gcl.springcloud.controller;

import com.gcl.springcloud.entities.CommonResult;
import com.gcl.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

    private static final String PAYMENT_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;


    @GetMapping(value = "/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject( PAYMENT_URL + "/payment/create" , payment , CommonResult.class);
    }

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id ){
        return restTemplate.getForObject( PAYMENT_URL + "/payment/get/" + id , CommonResult.class);
    }

}

RestTemplate启用@LoadBalanced负载均衡

package com.gcl.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    
}

当前的架构

需要注意的是虽然我们使用的是Eureka的集权架构,但是当前提供服务的支付模块只有一个。还体现不出来@LoadBalance的负载均衡,我们下一篇就实现多个课提供服务的负载均衡案例。
在这里插入图片描述

运行截图

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水而愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值