SpringCloud-8-Spring Cloud Feign

Spring Cloud Feign

 

 

 

Spring Cloud Feign 可以理解为封装了Ribbon+RestTemplate 

以往的开发中需要使用RestTemplate 中提供的get/post方法非常复杂

采用Spring Cloud Feign以后可以大幅度简化快发

1.在启动项中开启Feign

package com.mengxiangnongfu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
//开启Feign支持
@EnableFeignClients
public class ApplicationMain {

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

}

2.在service中调用服务中的方法

value对应的是eureka中服务的名称

package com.mengxiangnongfu.service;

import com.mengxiangnongfu.entity.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "PAYMENT")
public interface PayFeignService {

    @RequestMapping("/pay/get/{id}")
    public CommonResult getPayById(@PathVariable(value = "id") Long id);

}

3.controller

package com.mengxiangnongfu.controller;

import com.mengxiangnongfu.entity.CommonResult;
import com.mengxiangnongfu.service.PayFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderFeignController {

    @Resource
    private PayFeignService payFeignService;

    @RequestMapping("/pay/get/{id}")
    public CommonResult getPayById(@PathVariable(value = "id") Long id) {
        log.info("--------------->");
        return payFeignService.getPayById(id);
    }


}

 

pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>project</artifactId>
        <groupId>com.mengxiangnongfu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>feign-order</artifactId>


    <dependencies>
        <dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>com.mengxiangnongfu</groupId>
            <artifactId>commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

application.yml

 

server:
  port: 81
spring:
  application:
    name: feign-order
eureka:
  client:
  #这里就不把消费者注册到服务中心当中 register-with-eureka: false fetch-registry: true service-url: #defaultZone: http://127.0.0.1:7001/eureka #单机版 defaultZone: http://127.0.0.1:7001/eureka,http://127.0.0.1:7002/eureka #集群版

 

posted @ 2020-09-04 15:57  洋三岁  阅读(97)  评论(0)    收藏  举报
友情链接: 梦想农夫