以下是netcore 如何註冊eurake和通過zuul進行調用的教程方法。以及springcloud-eurake和springcloud-zuul的centos7安裝教程。
引用包:Steeltoe.Discovery.ClientCore
註冊包:
public void ConfigureServices(IServiceCollection services)
{
services.AddDiscoveryClient(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
// Add Steeltoe Discovery Client service
app.UseDiscoveryClient();
}
增加配置:
"spring": {
"application": {
"name": "TestUserApi"
}
},
"eureka": {
"client": {
"serviceUrl": "http://192.168.86.129:9000/eureka/",
"shouldFetchRegistry": false,
"shouldRegisterWithEureka": true
},
"instance": {
"port": 11002,
"preferIpAddress": true,
"instanceId": "192.168.86.129:11002"
}
}
如果開啓日誌:
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
//打開日誌
"Steeltoe": "Debug"
}
}
結果:打開地址 http://192.168.86.129:9000/ 如下圖,如果服務已經被註冊則完成
調用zuul地址:ip:9999/[服務名]/接口地址
如:http://192.168.86.129:9999/testuserapi/User/Login?user=123&pwd=5675676
注: 應用名稱 servicehall 必須爲小寫才能被調用,如果大寫或者駝峯均返回404
zuul 配置文件application.properties:
如:
spring.application.name=service-gateway
server.port=9999
ribbon.eureka.enabled=true
ribbon.listOfServers=http://localhost:9107
#ribbon的時間的四倍要小於hystrix-->timeoutInMilliseconds
#處理超時時間 默認5秒
ribbon.ReadTimeout=3000
#鏈接超時時間 默認2秒
ribbon.ConnectTimeout=3000
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/
zuul.host.socket-timeout-millis=60000
zuul.host.connect-timeout-millis=10000
zuul.sensitive-headers=true
#斷路切換超時
feign.hystrix.enabled=true
hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
#zuul.routes.order.path=/order/**
#zuul.routes.order.serviceId=order
----------------------------------------------------------------------------------------------------------------
如果是zuul(網關)的超時時間需要設置zuul、hystrix、ribbon等三部分:
zuul超時設置
#zuul超時設置
#默認1000
zuul.host.socket-timeout-millis=2000
#默認2000
zuul.host.connect-timeout-millis=4000
hystrix超時設置
#熔斷器啓用
feign.hystrix.enabled=true
hystrix.command.default.execution.timeout.enabled=true
#斷路器的超時時間,下級服務返回超出熔斷器時間,即便成功,消費端消息也是TIMEOUT,所以一般斷路器的超時時間需要大於ribbon的超時時間,ribbon是真正去調用下級服務
#當服務的返回時間大於ribbon的超時時間,會觸發重試
#斷路器的超時時間默認爲1000ms,太小了
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000
#斷路器詳細設置
#當在配置時間窗口內達到此數量的失敗後,進行短路。默認20個)
#hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
#短路多久以後開始嘗試是否恢復,默認5s)
#hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5
#出錯百分比閾值,當達到此閾值後,開始短路。默認50%)
#hystrix.command.default.circuitBreaker.errorThresholdPercentage=50%
ribbon超時設置
#ribbon請求連接的超時時間,限制3秒內必須請求到服務,並不限制服務處理的返回時間
ribbon.ConnectTimeout=3000
ribbon.SocketTimeout=5000
#請求處理的超時時間 下級服務響應最大時間,超出時間消費方(路由也是消費方)返回timeout
ribbon.ReadTimeout=5000
# 單獨設置某個服務的超時時間,會覆蓋其他的超時時間限制,服務的名稱以註冊中心頁面顯示的名稱爲準,超時時間不可大於斷路器的超時時間
#service-a.ribbon.ReadTimeout=50000
#service-a.ribbon.ConnectTimeout=50000
重試機制
#重試機制
#該參數用來開啓重試機制,默認是關閉
spring.cloud.loadbalancer.retry.enabled=true
#對所有操作請求都進行重試
ribbon.OkToRetryOnAllOperations=true
#對當前實例的重試次數
ribbon.MaxAutoRetries=1
#切換實例的重試次數
ribbon.MaxAutoRetriesNextServer=1
#根據如上配置,當訪問到故障請求的時候,它會再嘗試訪問一次當前實例(次數由MaxAutoRetries配置),
#如果不行,就換一個實例進行訪問,如果還是不行,再換一次實例訪問(更換次數由MaxAutoRetriesNextServer配置),
#如果依然不行,返回失敗信息。
linux系統JDK離線安裝
系統環境爲centos7.5 親測有效。
1.把安裝包拷貝到(/usr/local/software)目錄下(目錄自定義)
2.解壓縮
tar -zxvf jdk-8u171-linux-x64.tar.gz
3.配置環境
vi /etc/profile
4.編輯profile文件,在末尾添加
export JAVA_HOME=/usr/local/software/jdk1.8.0_171
export JRE_HOME=/usr/local/software/jdk1.8.0_171/jre
export PATH=$PATH:/usr/local/software/jdk1.8.0_171/bin
5.使修改生效
source /etc/profile