129、springcloud-eureka-client微服務的互相呼叫

香港胖仔發表於2020-04-02

這裡要寫的是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、測試呼叫

相關文章