Springcloud体系中 注册中心目前主流的有多种方案如 eureka、zookeeper、Consul等。
eureka:Springcloud1.0版本的黄金搭档,但目前已宣布停止更新了。
zookeeper:拥有完整的生态体系和社区资源,但和springcloud 契合度不高,灵活性较差。
Consul: 目前Consul 自身功能,和springcloud 对其集成的支持都相对较为完善,而且运维的复杂度较为简单。
因此在2.0后 普遍使用consul作为体系中的注册中心。consul要作为独立的程序部署,再在客户端中引入jar包,进行远程通讯。
使用wget加粗样式下载安装包 官方地址:https://www.consul.io/downloads.html
完成后将文件 解压
unzip consul_1.1.0_linux_amd64.zip
得到一个文件consul
设置环境变量
vi /etc/profile
根据路径写入
export CONSUL_HOME=/springcloud/consul/
export PATH=$PATH:$MAVEN_HOME/bin:$CONSUL_HOME
保存
source /etc/profile
生产环境
使用nohup 后台启动
对于运行日志,可是使用dev/null 直接抛弃,或者使用定时任务 切分文件
nohup consul agent -server -ui -bootstrap-expect=1 -data-dir=/datadish/lost+found/consul/data -node=lhm-springcould-consul -advertise=47.103.37.69 -bind=0.0.0.0 -client=0.0.0.0 &
下面是我自己写的定时任务,可做参考
# Author: Lhm
# Last Update: 2018.02.24
# Description: nohup.out 日志分割
this_path='/datadish/lost+found/sonsul/data/log' #根据脚本所在路径
current_date=`date -d "-1 day" "+%Y%m%d"` #列出时间
cd $this_path
echo $this_path
echo $current_date
do_split () {
[ ! -d logs ] && mkdir -p logs
split -b 10m -d -a 4 ./nohup.out ./logs/nohup-${current_date} #切分10兆每块至logs文件中,格式为:nohup-xxxxxxxxxx
if [ $? -eq 0 ];then
echo "Split is finished!"
else
echo "Split is Failed!"
exit 1
fi
}
do_del_log() {
find ./logs -type f -ctime +7 | xargs rm -rf #清理7天前创建的日志
cat /dev/null > nohup.out #清空当前目录的nohup.out文件
}
if do_split ;then
do_del_log
echo "nohup is split Success"
else
echo "nohup is split Failure"
exit 2
fi
# crontab -e 添加定时任务:每周第一天的1点执行一次
#0 1 * * */1 /server/scripts/clearNohup.sh &>/dev/null
对于consul启动的命令,可以查看下面这个链接
https://blog.csdn.net/liuzhuchen/article/details/81913562
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-consul-discovery
spring:
cloud:
consul:
host: 192.168.125.125
port: 8500
enabled: true
discovery:
enabled: true
serviceName: security
port: ${server.port}
health-check-path: /actuator/health
health-check-interval: 10s
tags: security
prefer-ip-address: true
package com.lhm.springcloud.security.config;
import com.ecwid.consul.v1.ConsulClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
import org.springframework.cloud.consul.discovery.TtlScheduler;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName RpsConsulServiceRegistryConfig
* @Author liuheming
* @Date 2019/4/26 15:07
* @Version 1.0
**/
@Configuration
public class RpsConsulServiceRegistryConfig {
@Autowired(required = false)
private TtlScheduler ttlScheduler;
@Bean
public MyConsulServiceRegistry consulServiceRegistry(
ConsulClient consulClient,
ConsulDiscoveryProperties properties,
HeartbeatProperties heartbeatProperties) {
return new MyConsulServiceRegistry(
consulClient, properties, ttlScheduler, heartbeatProperties);
}
class MyConsulServiceRegistry extends ConsulServiceRegistry {
public MyConsulServiceRegistry(ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {
super(client, properties, ttlScheduler, heartbeatProperties);
}
@Override
public void register(ConsulRegistration reg) {
reg.getService().setId(reg.getService().getName() + "-" + reg.getService().getAddress() + "-" + reg.getService().getPort());
super.register(reg);
}
}
}
https://github.com/liuheming/springcloudDemo.git