码迷,mamicode.com
首页 > 编程语言 > 详细

<Spring Cloud>入门三 Feign

时间:2019-01-13 01:53:51      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:png   style   add   java   面向   http   feign   mapping   dep   

1.Feign

  之前使用的是Ribbon+RestTemplate调用,通过的是微服务的名字进行调用,实现负载均衡

  技术分享图片

  但是为了满足接口编程,提供了Feign

2.实现

2.1引入坐标

  在 ms-common-api 和 ms-consumer-dept-80-feign 引入坐标

        <!--feign 客户端负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.2 在 ms-common-api 实现调用

package org.maple.service;

import org.maple.entity.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author mapleins
 * @Date 2019-01-12 23:10
 * @Desc 通过接口和注解 面向接口编程访问微服务
 **/
@FeignClient("ms-provider-dept")
public interface DeptClientService {

    @PostMapping("/dept/add")
    boolean add(@RequestBody Dept dept);

    @GetMapping("/dept/get/{id}")
    Dept get(@PathVariable("id") Long id);

    @GetMapping("/dept/list")
    List<Dept> list();

}

2.3修改 dept-80-feign的启动类

package org.maple;

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

/**
 * @author mapleins
 * @Date 2019-01-09 20:26
 * @Desc
 **/
@SpringBootApplication
@EnableEurekaClient 
@EnableFeignClients(basePackages = {"org.maple.service"}) //扫描ms-common-api中的service包
public class App_Consumer_Dept_80_Feign {

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

2.4 controller层

这样就相当于不是去调用微服务去编程,而是通过controller调用service层,实现接口编程,并且自带ribbon的轮询算法

package org.maple.controller;

import org.maple.entity.Dept;
import org.maple.service.DeptClientService;
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.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author mapleins
 * @Date 2019-01-09 20:10
 * @Desc
 **/
@RestController
public class DeptController_Consumer {

    @Autowired
    private DeptClientService service;

    @GetMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return service.get(id);
    }

    @GetMapping("/consumer/dept/list")
    public List<Dept> list(){
        return service.list();
    }

    @PostMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return service.add(dept);
    }

}

 

<Spring Cloud>入门三 Feign

标签:png   style   add   java   面向   http   feign   mapping   dep   

原文地址:https://www.cnblogs.com/mapleins/p/10261520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!