Spring Cloud之feign调用

Spring Cloud之feign调用

2018-1-30
liguo.wang@hand-china.com

feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

准备工作

服务注册中心

先创建一个服务注册中心,IDEA中先在父项目中new一个module:
这里写图片描述
然后next:
这里写图片描述
next,然后选择Cloud Discovery,选择Eureka Server,再next,finish。
这里写图片描述

然后再在src中的EurekaServerApplication中加上@EnableEurekaServer注解,表明它是一个注册中心
这里写图片描述

最后在resources目录下的application.properties中写入:

server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/

之后可以启动注册中心,浏览器输入localhost:8761,可以看到暂时是没有注册的
这里写图片描述

创建一个服务提供者

新建一个module,创建一个服务提供者eureka-client,过程和上面创建注册中心可以是一样的。
然后在EurekaClientApplication中:

package com.springcloud.eurekaclient;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

在resources目录下的application.yml(后缀改名为yml文件,这种写法感觉更好用)

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762

spring:
  application:
    name: service-A

这时可以新建controller层和service层,写几个服务方法:
这里写图片描述
比如 serviceClientController:

package com.springcloud.eurekaclient.Controller;

import com.springcloud.eurekaclient.Service.IFeignSevice;
import com.springcloud.eurekaclient.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright Shanghai Hand Co. Ltd.
 *
 * @author liguo.wang@hand-china.com
 * @date 2018/1/29 14:24
 * @version: 1.0
 * @description
 */
@RestController
public class serviceClientController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private IFeignSevice feignSevice;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String getServiceClientPort(){

        return "我是服务提供者的第一个方法" + feignSevice.getMsg(port);
    }

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String getServiceClientPort2(String name){
        return "我是服务提供者的第二个方法," + name + feignSevice.getMsg(port);
    }

    @RequestMapping(value = "/helloworld",method = RequestMethod.POST)
    public String getService(@RequestBody User user){
        return "我是服务提供者的第三个方法," + feignSevice.getMsg2(port,user);
    }
}

service实现类:

package com.springcloud.eurekaclient.Service.FeignServiceImpl;

import com.springcloud.eurekaclient.Service.IFeignSevice;
import com.springcloud.eurekaclient.dto.User;
import org.springframework.stereotype.Service;

/**
 * Copyright Shanghai Hand Co. Ltd.
 *
 * @author liguo.wang@hand-china.com
 * @date 2018/1/29 19:39
 * @version: 1.0
 * @description
 */
@Service
public class FeignServiceImpl implements IFeignSevice {
    @Override
    public String getMsg(String port) {
        return " 我的端口号是:"+port;
    }

    @Override
    public String getMsg2(String port, User user) {
        String msg = " 我的端口号是:"+port+" 姓名"+user.getUserName()+" 身高"+user.getHeight()+" 年龄"+user.getAge();
        return msg;
    }
}

然后启动项目,浏览器访问第一个方法,因为其他两个方法需要参数,然后我们会在后面使用feign来调用这两个方法。
可以看到,服务注册到注册中心了
这里写图片描述
然后访问浏览器地址:
这里写图片描述

创建一个feign服务

新建模块的过程和前面的类似,只是pom文件引入的依赖不一样,如图:
这里写图片描述
然后 ServiceFeignApplication:

package com.springcloud.servicefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

application.yml:

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

然后在feign服务里面创建controller层和service层:
这里写图片描述
frignController:

package com.springcloud.servicefeign.Controller;

import com.springcloud.servicefeign.Service.IFeignService;
import com.springcloud.servicefeign.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright Shanghai Hand Co. Ltd.
 *
 * @author liguo.wang@hand-china.com
 * @date 2018/1/29 17:09
 * @version: 1.0
 * @description
 */
@RestController
public class frignController {

    @Autowired
    private IFeignService feignService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String getMsg(){
        return feignService.getMsg();
    }

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String getServiceClientPort(){
        String name = "我是帅哥";
        return feignService.getServiceClientPort(name);
    }

    @RequestMapping(value = "/hello")
    public String getServiceClientPort2(){
        User user = new User().setUserName("wlg").setHeight(174).setAge(18);
        return feignService.getServiceClientPort2(user);
    }
}

IFeignService:

package com.springcloud.servicefeign.Service;

import com.springcloud.servicefeign.dto.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Copyright Shanghai Hand Co. Ltd.
 *
 * @author liguo.wang@hand-china.com
 * @date 2018/1/29 17:22
 * @version: 1.0
 * @description
 */
@FeignClient(value = "service-A")
public interface IFeignService {

    @RequestMapping(value = "/")
    String getMsg();

    @RequestMapping(value = "/hi")
    String getServiceClientPort(@RequestParam("name") String name);

    @RequestMapping(value = "/helloworld")
    String getServiceClientPort2(@RequestBody User user);
}

通过注解@FeignClient(value = “service-A”)找到服务提供者service-A,再通过@RequestMapping(value = “”)找到要调用的方法,
这里的例子举了调用GET方法和POST方法。feign调用的一方记得加上注解,比如说get请求的@RequestParam注解,@PathVariable注解等等,
post请求记得加上@RequestBody注解等等。被feign调用的一方由于是controller层,所以带参数的话这些注解按道理是有的,
但被调用的一方的controller的某个GET请求接口如果仅仅只是用来被feign调用,我发现在这边的参数不写注解也是行的。比如:
这里写图片描述
启动项目,看注册中心:
这里写图片描述
浏览器再分别访问localhost:8765的三个接口:
这里写图片描述
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值