SpringCloud快速开始 | Feign服务调用(二)Greenwich版本

源码地址

原文地址:莫问博客

一、Feign介绍
  Feign是一个声明式的伪Http客户端,通过Feign可以实现服务间的相互调用,比如服务A调用服务B暴露的一些接口;同时Feign整合了Ribbon,所以Feign也可以实现服务的负载均衡调用。想要使用Feign也比较简单,定义一个通过注解@FeignClient()指定需要调用的服务的接口,启动类加上@EnableFeignClients开启Feign功能即可

二、创建external-api-service工程,加入依赖

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

   2.1、启动后加入@EnableFeignClients @EnableEurekaClient,启动类代码如下

     

package com.lxd.external;

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

/**
 * @author liuxiaoding
 * @data 2019/7/4
 */
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
//@EnableCircuitBreaker
public class ExternalApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExternalApplication.class,args);
    }
}

2.2、Application.yml文件如下

server:
  port: 8003
logging:
  path: ./logs/
  level:
    root: info
spring:
  application:
    name: external-api-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7081/eureka/,http://localhost:7082/eureka/,http://localhost:7083/eureka/

2.3、新建UserRemoteService.java,来演示Feign调用,代码如下

package com.lxd.external.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author liuxiaoding
 * @data 2019/7/4
 */
@FeignClient(name = "user-service")
public interface UserRemoteService {

    @RequestMapping(value = "getUserBaseInfo")
    JSONObject getUserBaseInfo();

}

2.4、新建UserInfoController,代码如下

package com.lxd.external.controller;

import com.alibaba.fastjson.JSONObject;
import com.lxd.external.remote.UserRemoteService;
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;


/**
 * @author liuxiaoding
 * @data 2019/7/4
 */
@RestController
public class UserInfoController {
    @Autowired
    private UserRemoteService userRemoteService;

    @RequestMapping(value = "getUserBaseInfo", method= RequestMethod.GET)
    public JSONObject getUserBaseInfo(){
       return userRemoteService.getUserBaseInfo();
    }
}

三、服务提供者

     3.1、在上文的user-service1、user-service2中,新建UserInfoController,代码如下,注意user-service1、user-service2的name返回值不一样,用来检测是否服务已经被负载

package com.lxd.user.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liuxiaoding
 * @data 2019/7/4
 */
@RestController
public class UserInfoController {

    @RequestMapping(value = "getUserBaseInfo", method= RequestMethod.GET)
    public JSONObject getUserBaseInfo(){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("name","用户服务-2");
        jsonObject.put("index",2);
        return  jsonObject;
    }
}

   3.2、依次启动external-api-service、user-service1、user-service2,打开eureka http://localhost:7081/查看是否都注册上去了

可以发现已经都注册上去了

四、访问external-api-service服务的getUserBaseInfo接口,http://localhost:8003/getUserBaseInfo

     刷新浏览器发现{"name":"用户服务-2","index":2}和{"name":"用户服务-1","index":2}来回变换,可以知道服务调用已生效,并且Feign的负载均衡也已生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值