一、服务端:需要增加配置中心服务器config-server工程;
1、pom.xml增加相关配置信息
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
2、启动类中增加
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
3.yml配置文件
server:
port: 8707
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
search-locations:
- file:/home/wshop/service/config
#- classpath:/config
# git:
# uri: ssh://git@127.0.0.1:61317/home/git/repo/java/cloud-config
# searchPaths: dev
# lable: master
# strict-host-key-checking: false
# ignore-local-ssh-settings: true
# private-key: |
# -----BEGIN RSA PRIVATE KEY-----
# MIIEpQIBAAKCAQEAx8S2qMyPTZxyw6+OB7PSLRM5WH+e1FPaKiqYwLzj1v0f9yGP
# 2cf41Si8m1y7o/tyaqoNiHbVkWlBvS5Pn6JCD3hlNQxCump2ZG2XqGg46EdDt52E
# PWB2u4flSqzp4hMN+/5ChY1FTCg7IXY6uOxJ3U864MuZ6ChumxfgyYCmLb7mkX+D
# S8vmDqNx5AGn9UscrhhdTuMMyw6HolUMdCQmpSyNyWQWk9caVKOvQlTGKG2nCAah
# apGHShjghpCsASGo+qk7/af/ZrRoJEj2eeu8MCLBcdyd5MOZhf/xr3wCXIZItxCJ
# P2qyPDg8s6umv6oW7nsKsy0qUcovZTqp1XkAawIDAQABAoIBABsuO8Ap3RGtSLwx
# ZbzMjbtOU04kRyMSqat1EkWfLczxsei5K05xc/zkxwzIiEteignTS3YW3SYk0SoB
# ZBoPRLfAJN0b5vRgSe2OL8VdMOkB0UfpabXEK0OG0P7hGg2FlDCGPtzXBOfAzZLW
# +FRy6fXvss4lRbKd1bbKf+5Kz9GHWy8gKWnpm9CXhnGvqCrxMkkBWuDOwpRK8VSx
# NIUj6wZwtpSOqWJPLzB3ZCrLKhFxmaHA8cYZdiFp/5P/N5Hq+9zy+uFReCfhcr33
# d7KgLnD1hnQdkuY7O2Jg9PTJUlxb41+W0WY4ZYW5igvtk8zirperuLbeHBbpQvsx
# 6CBx8akCgYEA9WZP9MF/joMHdtt0Mn663ycL9PEA233psnLghxeKNXR/n22MtK9Y
# CAnHO9mNiBp7nUUDKRtGwsUytUQt0BcuhikxisY+WoeKv1lmLperz6Qv7n4TTN3D
# 7edX6lQVLV8Q0k26Cf6kKEf9Noy0Utz8AWEMzK9XwMzIVa5+RB14t9cCgYEA0GXM
# z3MZvEe7IQnQGNvWzRFZ3lQEmpOUzzluOj+fBNh9A8v4+U5GAw7QQop5L5//rD7n
# 7DUhl5BoZOhUzdBTQr6q4rs05EBphPrwZgpGGDWXmmBzMD88fM5j1bKOC2c3ZqLs
# TVOdWTsj+iNqvxnfvMYB6U5PdM06wVObM0Q+WY0CgYEAqOlxo7EE0d1S5/cKNi+R
# stbDfNooI8Rx7a7WA26WMHOzuf5uyaeulNiYZf34dR86X6ak0xv98szUTE8XP5R1
# kP5LowgWKQFskV3CEiQLKCHodU7omUI1p91M99IBvQzu87A6ffm7cW8=
# -----END RSA PRIVATE KEY-----
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8700/eureka/
registry-fetch-interval-seconds: 10
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 10
lease-renewal-interval-in-seconds: 10
4.在linux目录“/home/wshop/service/config”下增加共享配置文件“base-dev.yml”:
文件内容比如:
# 配置hystrix
feign:
hystrix:
enabled: true
ribbon:
ReadTimeout: 400000
ConnectTimeout: 400000
# 配置hystrix
hystrix:
metrics:
enabled: true
threadpool:
default:
coreSize: 101 #线程池核心线程数
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 400000 #超时时间,如果超时,则实现服务降级处理
circuitBreaker:
requestVolumeThreshold: 100 #当在配置时间窗口内达到此数量后,进行短路。默认20个
sleepWindowInMilliseconds: 5000 #短路多久以后开始尝试是否恢复,默认5s
errorThresholdPercentage: 50% #出错百分比阈值,当达到此阈值后,开始短路。默认50%
management:
endpoint:
health:
show-details: always
web:
exposure:
include: hystrix.stream
4.run脚本启动,并访问测试
run脚本范例:
#!/bin/bash ######################################################################### # File Name: run.sh # Function: # Author: Mason # Version: V1.0 # Created Time: 28/1/2019 11:41:48 ######################################################################### # chkconfig: - 98 33 # description: Starts and stops the java project daemon \ # used to provide some java jar packet services. source /etc/profile # base env parameters setting BASEDIR=$(dirname $(readlink -f $0)) APPNAME=config-server VERSION=1.0.0 JAVAOPT="-Xms256m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=256M" # define some functions get_pid(){ PID=$(ps -ef|grep ${APPNAME} |grep "java" |grep -v grep |awk '{print $2}') } start(){ if [ -z ${PID} ];then nohup java ${JAVAOPT} -Dlogging.config=${BASEDIR}/logback-spring.xml -D$APPNAME -jar ${BASEDIR}/${APPNAME}-${VERSION}.jar --spring.config.location=${BASEDIR}/application.yml &>/dev/null & inter=1 time=10 i=0 while ((i < time));do get_pid #if [ $(netstat -lntup|grep -c $PID) -le 1 ];then if((i>(time-2)));then if [ ${PID} ];then echo "${APPNAME} started OK." break else sleep ${inter} let i++ fi else sleep ${inter} let i++ fi done if ((i == time));then echo "${APPNAME} started FAIL in $((inter*time)) second!" exit 1 fi else echo "${APPNAME} is still running with pid ${PID}!" exit 1 fi } stop(){ if [ ! -z ${PID} ];then kill -9 ${PID} && echo "${APPNAME} was killed." else echo "${APPNAME} is not running!" exit 1 fi } restart(){ stop start } status(){ if [ ! -z ${PID} ];then echo "${APPNAME} is running with pid ${PID}" else echo "${APPNAME} is not running." fi } # the main program started get_pid case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo $"Usage: $0 {start|stop|restart|status}" exit 2 esac exit $?
http://192.168.60.235:8707/base-dev.yml
说明服务端已经启动成功;
二、客户端
1.在linux目录“/home/wshop/service/config”下增加共享配置文件“bootstrap.yml”:
其内内容如下:
spring:
cloud:
config:
uri: http://127.0.0.1:8707/ #Config server的uri
profile: dev #指定的环境
name: base
启动客户端应用jar中增加配置:
/home/wshop/service/config/bootstrap.yml
完整启动命令:
nohup java ${JAVAOPT} -Dlogging.config=${BASEDIR}/logback-spring.xml -D$APPNAME -jar ${BASEDIR}/${APPNAME}-${VERSION}.jar --spring.config.location=${BASEDIR}/application.yml,/home/wshop/service/config/bootstrap.yml &>/dev/null &
重新启动客户端,即实现客户端应用接入配置中心功能。