您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關微服務spring-cloud 配置中心config-server本地化部署是怎樣的,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
一、服務端:需要增加配置中心服務器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 &
重新啟動客戶端,即實現客戶端應用接入配置中心功能。
上述就是小編為大家分享的微服務spring-cloud 配置中心config-server本地化部署是怎樣的了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。