原文地址:莫问博客
一、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的负载均衡也已生效