Eureka+feignCenlit多节点同步(补spring boot2)
利用Eureka完成多节点同步(spring boot1)
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
利用Eureka获取全部节点
appName表示为应用在eureka中注册名称
@Autowired
private EurekaClient eurekaClient;
@Autowired
private XxxConverters xxxConverters;
public void query(String code) {
List<InstanceInfo> instanceInfos = eurekaClient.getInstancesByVipAddressAndAppName(null, appName, false);
if (CollectionUtils.isEmpty(instanceInfos)) {
// throw new Exception();
}
for (InstanceInfo instanceInfo : instanceInfos) {
xxxConverters.queryForUrl(instanceInfo.getHomePageUrl(),code);
}
return tasks;
}
利用Feign轮询节点
动态生成feign客户端
@Component
@Import(FeignClientsConfiguration.class)
public class DynamicBuildFeignService {
@Autowired
private Decoder decoder;
@Autowired
private Encoder encoder;
public <T> T buildFeignClient(Class<T> feignClass, String url) {
return Feign.builder().decoder(decoder).encoder(encoder)
.target(feignClass, url);
}
}
转换控制类,主要职责是保存feign的客户端,不要每次都生成一个。
@Component
public class XxxConverters {
private Map<String, XxxFeignClient> feignClients = Maps.newHashMapWithExpectedSize(32);
@Autowired
private DynamicBuildFeignService dynamicBuildFeignService;
public XxxFeignClient getFeignClient(String url) {
XxxClient feignClient = feignClients .get(url);
if (null == feignClient) {
feignClient = dynamicBuildFeignService.buildFeignClient(XxxFeignClient.class, url);
feignClients.put(url, feignClient);
}
return feignClient;
}
public List<Xxx> queryForUrl(String url, String code) {
List<Xxx> result = getFeignClient(url).queryForThis(code);
return result ;
}
}
Feign客户端,此处需要注意RequestLine的使用。
@FeignClient(name = "appName")
public interface XxxFeignClient {
@RequestLine("GET /api/xxx/{code}")
List<Xxx> queryForThis(@Param("code") String code);
}
补充spring boot2
因为部分原因,公司升级了boot的版本。没几个月1也不维护了,这里升级spring boot2遇到了一些问题。在这里补充下,若没有变化的代码就不贴了。
spring升级的问题主要是pom的变化较大,cloud方面重构了结构,和以前区别较大。
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
动态生成feign客户端
升级之后decoder,encoder默认注入被改掉了。若使用默认的new Encoder.Default(),new Decoder.Default()的话是不支持复杂对象的解析,只能传传String之类的。
(主要变化点)动态生成feign客户端
@Component
@Import(FeignClientsConfiguration.class)
public class DynamicBuildFeignService {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
public <T> T buildFeignClient(Class<T> feignClass, String url) {
return Feign.builder().encoder(new SpringEncoder(messageConverters)).decoder(
new SpringDecoder(messageConverters))
.target(feignClass, url);
}
}