创建cloud-provider-payment8091

创建过程和cloud-provider-payment8090一样

cloud-provider-payment8091的pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>cloud2020</artifactId>
  7. <groupId>com.sgy.cloud2020</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>cloud-provider-payment8091</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>com.sgy.cloud2020</groupId>
  15. <artifactId>cloud-provider</artifactId>
  16. <version>${project.version}</version>
  17. </dependency>
  18. <!--eureka client-->
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <scope>provided</scope>
  27. </dependency>
  28. </dependencies>
  29. </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";

1610803428560.png

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();
    }
}

测试

  • 支付集群服务注册效果

1610803428589.png

  • 访问创建订单服务,会发现8090和8091支付服务会被轮询调用