SpringCloud-6-注册生产者pay的集群搭建

新建子工程 - > 同pay8001端口的子工程相同,修改配置文件

只需要修改相应的端口

server:
  port: 8002
spring:
  application:
    #微服务的名称
    name: payment
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/pay?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.mengxiangnongfu.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka #集群版

修改order 80端口消费者的调用地址为Eureka的服务名

package com.mengxiangnongfu.controller;

import com.mengxiangnongfu.entity.CommonResult;
import com.mengxiangnongfu.entity.Pay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;


@RestController
@Slf4j
public class OrderController {

  //这里使用Eureka的服务名
public static final String PAYMENT_URL = "http://PAYMENT"; //传统RestTemplate请求 @Autowired RestTemplate restTemplate; @GetMapping("/consumer/pay/create") public CommonResult<Pay> create(Pay pay) { log.info("----------->创建订单--->消费者"); return restTemplate.postForObject(PAYMENT_URL + "/pay/create", pay, CommonResult.class); } @GetMapping("/consumer/pay/get/{id}") public CommonResult<Pay> getPayById(@PathVariable(value = "id") Long id) { log.info("----------->查询订单--->消费者"); return restTemplate.getForObject(PAYMENT_URL + "/pay/get/" + id, CommonResult.class); } }

注意:

在RestTemplate上加上负载均衡的注解,否则会出现无法轮训调用 具体错误如图

注解 @LoadBalanced 开启集群

 

 

默认访问效果为轮训 8001端口一次 8002端口一次

posted @ 2020-08-30 13:56  洋三岁  阅读(121)  评论(0)    收藏  举报
友情链接: 梦想农夫