微服务学习之消费模块【Hoxton.SR1版】

目录

 

1 pom.xml

2 application.yml 

3 注入RestTemplate

4 controller


1 pom.xml



    
        cloud2020
        com.bighuan.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-order80

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            com.bighuan.springcloud
            cloud-api-commons
            ${project.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.bighuan.springcloud.OrderMain80
                
                
                    
                        
                            repackage
                        
                    
                
            
        
    

2 application.yml 

server:
  port: 80
spring:
  application:
    name: cloud-order-service
eureka:
  client:
    # 表示是否将自己注册进Eureka Server,默认为true
    register-with-eureka: true
     # 是否从Eureka Server获取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true,才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版

3 注入RestTemplate

@Configuration
public class ApplicationContextConfig {

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

4 controller

@RestController
@Slf4j
public class OrderController {
    // 单机
    // private static final String PAYMENT_URL = "http://localhost:8001";
    // 集群
    private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;


    @GetMapping(value = "/consumer/payment/create")
    public CommonResult create(Payment payment) {

        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping(value = "consumer/payment/get/{id}")
    public CommonResult getPaymentByid(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

 

 

 

 

你可能感兴趣的:(SpringCloud)