- 项目结构
说明:
cloud-eureka-server7001模块、cloud-eureka-server7002模块两个组成eureka注册中心集群
cloud-provider-payment8001模块、cloud-provider-payment8002模块两个组成支付模块服务集群
实现负载均衡:
1、配置
cloud-provider-payment8001:
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://106.54.196.44:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: password
eureka:
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.laoxu.springcloud.entities
cloud-provider-payment8002:
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://106.54.196.44:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: password
eureka:
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.laoxu.springcloud.entities
集群的spring.application.name必须要一致
2、修改消费微服务端restTemplate请求地址为注册中心服务即 spring.application.name
package com.laoxu.springcloud.controller;
import com.laoxu.springcloud.entities.CommonResult;
import com.laoxu.springcloud.entities.Payment;
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.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
// private static final String PAYMENT_URL = "http://localhost:8001";
private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@PostMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" +id, CommonResult.class);
}
}
3、为restTemplate方法添加负载均衡注解
package com.laoxu.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();
}
}
4、全部启动
5、测试
可以看到消费者在请求时注册中心会做负载均衡,默认的负载均衡机制为轮询访问所有的集群