使用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;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枣泥馅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值