eureka 已经逐渐干败zookeeper~
eureka: 注册中心
robbin: 负载均衡策略:找调那一台服务器
RestTemplate:封装工具类,调服务方接口**
话不多说,来兹狗!
1、引包
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR1</version>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
2、配置、注册到eureka,拉eureka上所有服务列表保存到client
app.env=dat
spring.application.name=${app.env}-panda-eureka-client
eureka.client.serviceUrl.defaultZone=http://10.0.0.1:18001/eureka/,http://10.0.0.2:18001/eureka/,http://10.0.0.3:18001/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
3、注入工具类
@Autowired
RestTemplate restTemplate;
4、调远程服务 参考代码step 1 2 3
业务场景:
我这是一个定时任务,调远程集群服务去跑业务逻辑,RPC通过eureka实现
package com.dj.internet.task.batch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisCluster;
/**
* 定时任务 - 批量清分
*/
@Component
public class BatchSortOutScheduleTask {
private static Logger logger = LoggerFactory.getLogger(BatchSortOutScheduleTask.class);
//step 1、注入封装工具类
@Autowired
RestTemplate restTemplate;
//step 2、读配置文件 - 获取注册在eureka上的服务应用名
@Value("${dj.service.sync.savepdf.serviceid}")
private String serviceId;
//step 2、读配置文件 - 获取接口路径
@Value("${dj.service.batch.savepdf}")
private String path;
@Value("${job.corn}")
private String jonCorn;
@Value("${app.env}")
private String env;
@Autowired
private JedisCluster jedisCluster;
private static String LOCK_NAME="Lock_BatchSortOutSchedule";
// 每天凌晨12点同步当天确认函号中间4位(不含4)最大值到缓存
@Scheduled(cron = "${job.corn}")
public void synMaxLetterNo() {
String lock=jedisCluster.set(env+"_"+LOCK_NAME,"1","NX","EX",30*60);
if ("OK".equalsIgnoreCase(lock)) {
try {
Long start = System.currentTimeMillis();
logger.info("定时任务 - 批量清分开始。。");
String url = "http://" + serviceId + "/" + path;
//step 3、调远程服务,当前post请求,返回类型ResponseEntity,body体获取返回数据
String result = restTemplate.postForEntity(url, null, String.class).getBody();
Long end = System.currentTimeMillis();
logger.info("定时任务 - 批量清分结束。。耗时:"+(end - start )+"ms");
} catch (Exception e) {
e.printStackTrace();
logger.info("定时任务 - 批量清分失败。。");
}finally {
if ("OK".equalsIgnoreCase(lock)) {
jedisCluster.del(env + "_" + LOCK_NAME);
}
}
}else {
logger.info("未获取锁");
}
}
}
结尾
希望对大家有帮助,如果喜欢的话记得点赞加关注哦
网友评论