Spring的feignClient调用说明

1.Feign接口定义

@FeignClient("sc-app-provider")
public interface SampleService {
    @RequestLine("GET /sample/{id}")
    String info(@Param("id") String id);
  
    @Headers({"Content-Type: application/json"})
    @RequestLine("POST /sample/add")
    JSONObject add(JSONObject user);
  
    @Headers("Content-Type: application/json")
    @RequestLine("PUT /sample/{key}")
    User put(@Param("key") String key, JSONObject value);
  
    @RequestLine("DELETE /sample/{id}")
    String delete(@Param("id") String key);
}

2 修改eureka.properties文件,增加调用的eureka相关配置信息

#服务不注册到eureka上
eureka.registration.enabled=false
eureka.preferSameZone=true
eureka.shouldUseDns=false
#注册到eureka上的地址
eureka.serviceUrl.default=http://discovery.xxxxxx.com:9401/eureka
#优雅停机参数配置registryFetchIntervalSeconds
eureka.client.refresh.interval=5

3增加ribbon.properties文件,配置调用的ribbon相关信息


# 调用的Rest服务的ServiceId
sc-app-provider.ribbon.DeploymentContextBasedVipAddresses=sc-app-provider
 
# 固定写法,使用的ribbon负载均衡器
sc-app-provider.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
 
# 每分钟更新对应服务的可用地址列表
sc-app-provider.ribbon.ServerListRefreshInterval=5000
#设置超时时间 可根据自己服务设置,尽量大
ribbon.ConnectTimeout=50000000
ribbon.ReadTimeout=50000000

4调用feign接口

@Namespace("/json/crm_sample")
public class SampleAction {
    protected static final String PAGES_COMMON_ROOT = "/WEB-INF/pages/common";
 
    private String id;
 
    @Autowired
    private XsyServiceLocator xsyServiceLocator;
 
    @Action(value = "info", results = {@Result(location = PAGES_COMMON_ROOT + "/onlyData.jsp")})
    public String info(){
        SampleService sampleService = xsyServiceLocator.lookup(SampleService.class);
        if(StringUtils.isBlank(id)){
            id = "xxx";
        }
        String result = sampleService.info(id);
        ServletActionContext.getRequest().setAttribute("data", "id="+result);
        return "success";
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值