美文网首页
分布式组件-feign-项目集成

分布式组件-feign-项目集成

作者: 乙腾 | 来源:发表于2021-02-11 00:03 被阅读0次

    配置步骤

    引入依赖

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

    EnableFeignClients

    package com.pl.gulimail.member;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @MapperScan("com.pl.gulimail.member.dao")
    @EnableFeignClients(basePackages = "com.pl.gulimail.member.feign")
    @EnableDiscoveryClient
    @SpringBootApplication
    public class GulimailMemberApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GulimailMemberApplication.class, args);
        }
    
    }
    

    FeignClient

    @FeignClient("服务名")

    package com.pl.gulimail.member.feign;
    
    import com.pl.common.utils.R;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    /**
     * @Auther: pl
     * @Date: 2020/7/24 04:03
     * @Description:
     */
    @FeignClient("gulimail-product")
    public interface ProductFeignService {
    
        @GetMapping("product/brand/getAll")
        public R getAll();
    
    
    }
    
    

    要远程调用的product服务的接口

    package com.pl.gulimail.product.controller;
    
    import com.pl.common.utils.PageUtils;
    import com.pl.common.utils.R;
    import com.pl.gulimail.product.entity.BrandEntity;
    import com.pl.gulimail.product.service.BrandService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Arrays;
    import java.util.Map;
    
    
    
    /**
     * 品牌
     *
     * @author pl
     * @email xxxx@163.com
     * @date 2020-06-28 23:43:54
     */
    @RestController
    @RequestMapping("product/brand")
    public class BrandController {
        @Autowired
        private BrandService brandService;
    
        @GetMapping("/getAll")
        public R getAll(){
            return R.ok().put("value",brandService.list());
        }
    
    }
    
    

    member的controller

    package com.pl.gulimail.member.controller;
    
    import java.util.Arrays;
    import java.util.Map;
    
    
    import com.pl.gulimail.member.feign.ProductFeignService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.pl.gulimail.member.entity.MemberEntity;
    import com.pl.gulimail.member.service.MemberService;
    import com.pl.common.utils.PageUtils;
    import com.pl.common.utils.R;
    
    
    
    /**
     * 会员
     *
     * @author pl
     * @email xxxx@163.com
     * @date 2020-07-03 18:13:32
     */
    @RestController
    @RequestMapping("member/member")
    public class MemberController {
    
        @Autowired
        ProductFeignService productFeignService;
    
        @GetMapping("/test/getBrandAll")
        public R getBrandAll(){
            //注意product中已经封装数据到json的value中,这里为了避免出现value套value,再返回的json中直接获取value
            return R.ok().put("value",productFeignService.getAll().get("value"));
        }
    }
    
    

    postman

    image.png

    相关文章

      网友评论

          本文标题:分布式组件-feign-项目集成

          本文链接:https://www.haomeiwen.com/subject/ahihxltx.html