使用Ribbon和Feign消費Eureka中的服務

7.1. How to Include Ribbon

To include Ribbon in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-ribbon. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

在官網中提到如果使用Ribbon就添加

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

其實除了添加這個還需要添加Eureka的client是用來發現Eureka中的服務的

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign消費服務的包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--ribbon消費服務的包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

application.yml

spring:
  application:
    name: order-service
server:
  port: 8781
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

在啓動類中加入:

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

在需要它的類中引入它就可以了

public class OrderController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private ProductClient productClient;
//    (name = "save",produces ={MediaType.APPLICATION_PROBLEM_JSON_VALUE})
    @RequestMapping(value = "save",produces =MediaType.APPLICATION_PROBLEM_JSON_VALUE)
    public  Map<String,String> save(Integer id){
        Map<String,String> result=new HashMap<>();
        Product pro = restTemplate.getForObject("http://product-service/api/product/findById?id=" + id, Product.class);
        Product product = productClient.findById(id);
        System.out.println("feign 獲取到的product:"+product.getName());
        result.put("code","1");
        result.put("data","success");
        result.put("message","訂單提交成功");
        result.put("product_name",pro.getName());
        return result;
    }
}

 

1.1. How to Include Feign

To include Feign in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-openfeign. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

<!--feign消費服務的包-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

然後在啓動類上加入註解@EnableFeignClients

然後編寫Feign相關的client

package com.example.eurekaorderservice;

import com.example.eurekaorderservice.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name="product-service")
public interface ProductClient {
    @GetMapping("/api/product/findById")
    public Product findById(Integer id);
}

在使用到它的地方調用它即可

package com.example.eurekaorderservice.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.eurekaorderservice.ProductClient;
import com.example.eurekaorderservice.domain.Product;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RequestMapping("/api/order")
@RestController
public class OrderController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private ProductClient productClient;
//    (name = "save",produces ={MediaType.APPLICATION_PROBLEM_JSON_VALUE})
    @RequestMapping(value = "save",produces =MediaType.APPLICATION_PROBLEM_JSON_VALUE)
    public  Map<String,String> save(Integer id){
        Map<String,String> result=new HashMap<>();
        Product pro = restTemplate.getForObject("http://product-service/api/product/findById?id=" + id, Product.class);
        Product product = productClient.findById(id);
        System.out.println("feign 獲取到的product:"+product.getName());
        result.put("code","1");
        result.put("data","success");
        result.put("message","訂單提交成功");
        result.put("product_name",pro.getName());
        return result;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章