创建cloud-provider-payment8091
创建过程和cloud-provider-payment8090
一样
改cloud-provider-payment8091
的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.sgy.cloud2020</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8091</artifactId>
<dependencies>
<dependency>
<groupId>com.sgy.cloud2020</groupId>
<artifactId>cloud-provider</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
改支付服务8091,主启动类
package com.sgy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Created by AaronShen on 2020/5/27
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8091 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8091.class,args);
}
}
改cloud-provider-payment8091配置文件
server:
port: 8091
spring:
application:
name: cloud-payment-server #这里的名称和8090工程中的配置文件一样
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7002.com:7002/eureka,http://eureka7001.com:7001/eureka/ #集群版
#defaultZone: http://eureka7001.com:7001/eureka/
修改订单cotroller
将访问的端口号以及ip替换成CLOUD-PAYMENT-SERVER";
package com.sgy.springcloud.controller;
import com.sgy.springcloud.entites.Payment;
import com.sgy.springcloud.entities.R;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* Created by AaronShen on 2020/5/26
*/
@Slf4j
@RestController
public class OrderController {
private final static String PATMEMT_URL = "http://CLOUD-PROVIDER-PAYMENT";
@Resource
RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public R create(@RequestBody Payment payment) {
return restTemplate.postForObject(PATMEMT_URL + "/payment/create",payment,R.class);
}
@GetMapping("/consumer/payment/find/{id}")
public R find(@PathVariable(value = "id") String id) {
return restTemplate.getForObject(PATMEMT_URL + "/payment/find/" + id,R.class);
}
@GetMapping("/consumer/page")
public R page() {
log.info("分页查询");
return restTemplate.getForObject(PATMEMT_URL + "/payment/page",R.class);
}
}
为订单服务中的RestTemplate加负载均衡注解
package com.sgy.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;
/**
* Created by AaronShen on 2020/5/26
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
测试
- 支付集群服务注册效果
- 访问创建订单服务,会发现8090和8091支付服务会被轮询调用