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;
}
}