SpringCloud项目从零开始(六)---msc-consumer调用msc-provider

说明

本篇文章主要配置consumer如何调用provider的。
微服务中,项目之间的调用使用http协议进行处理的。

msc-consumer

配置RestTemplate

创建配置类,配置管理RestTemplate,在项目中使用RestTemplate去访问msc-provider接口。

package team.one.msc.consumer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * Created by lankk on 2020/5/14.
 *
 * @author lankk
 * @date 2020/5/14
 */
@Configuration
public class RestTmplateConfig {

    /**
     * 注解 @Bean 表示这是由spring管理的类,返回值 RestTemplate 就是要管理的类,方法名 restTemplate 是类的id
     * <p>
     * 和下面在xml文件的配置相同
     * <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

ConsumController改动

package team.one.msc.consumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by lankk on 2020/5/12.
 *
 * @author lankk
 * @date 2020/5/12
 */
@Slf4j
@RestController
@RequestMapping("/csm")
public class ConsumController {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${provider.url}")
    private String providerUrl;

    /**
     * 简单测试
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public String getId(@PathVariable int id) {
        log.info("ConsumController.getId,入参id={}", id);

        // 访问msc-provider
        String result = restTemplate.getForObject(providerUrl.concat("/pvd/test/").concat(String.valueOf(id)), String.class);
        log.info(result);
        return "传参:" + id + ",返回:" + result;
    }

}

yml配置


server:
  # 本服务的端口号
  port: 8080

spring:
  application:
    # 服务名称,集群中的每个实例的服务名称是相同的
    name: msc-consumer

eureka:
  client:
    # 消费者需要向eureka server注册,以后Gateway要向server拉取消费者实例
    register-with-eureka: true
    # 从eureka server中拉取注册的eureka client实例信息
    fetch-registry: true
    service-url:
      # 消费者需要向eureka server获取生产者地址,多个之间使用【,】隔开
      defaultZone: http://eureka01:7001/eureka/
  instance:
    instance-id: 消费者01


# 自定义属性
provider:
  # msc-provider的地址
  url: http://localhost:18080/

启动测试

启动msc-eureka-server、msc-consumer和msc-provider项目。
在这里插入图片描述
浏览器中输入 http://localhost:8080/csm/get/561 访问。
在这里插入图片描述

项目源码

我的GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值