129、springcloud-eureka-client微服务的互相调用

这里要写的是springboot 订单模块的服务去调用商品模块的服务进行下单操作

下面首先进行订单模块的代码编写

1、将order订单模块注册到注册中心

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8181/eureka
spring:
  application:
    name: eureka-client-orders
server:
  port: 8185

2、pom.xml文件中添加

springcloud的feign依赖和

springboot的web依赖项

springcloud负载均衡的ribbon依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

 

3、在主类中创建负载均衡的restTemplate字段

package pafc.cloud.eurekaclientorder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
public class EurekaClientOrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientOrderApplication.class, args);
    }


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

}

 

 

4、微服务之间的调用,使用restTemplate进行调用

package pafc.cloud.eurekaclientorder.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import pafc.cloud.eurekaclientorder.domain.GoodItem;
import pafc.cloud.eurekaclientorder.domain.Order;
import pafc.cloud.eurekaclientorder.service.OrderService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class OrderServiceImpl implements OrderService {

    @Autowired
    public RestTemplate restTemplate;



    @Override
    public Object createOrder() {
        Object allGoods = restTemplate.getForObject("http://eureka-client-goods/queryGoods/allGoods", Object.class);
        Order orderItem = new Order();
        List<Object> goodsLists = new ArrayList<Object>();
        Map<String, GoodItem> allGoodsMap = (Map<String, GoodItem>) allGoods;
        for (Map.Entry<String, GoodItem> item : allGoodsMap.entrySet()) {
            goodsLists.add(item.getValue());
        }
        orderItem.setGoodsList(goodsLists);
        orderItem.setId("001");
        orderItem.setName("第一单");
        orderItem.setPrice(98880.0);
        return orderItem;
    }
}

 

 

5、测试调用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值