日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用配置项

發(fā)布時間:2024/4/13 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用配置项 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
寫到代碼里都是寫死的,我能不能寫到配置項里面去呢,沒錯,這也是可以的,我們先從上面的超時開始,我把這個也注釋掉,我把超時拷貝一下,寫到配置項里面,為了演示,我先寫到本地的文件,就先不放到統(tǒng)一配置中心了,拷貝過來之后就是這個樣子的hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 3000hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000這里前面還要配置一些參數(shù),用yml配置這個的時候真不美觀,如果是properties方式的話,那么一行就寫完了,這個跟走樓梯一樣的,還是得這么配,我們再來啟動一下,大家覺得這樣的配置能成功嗎,我們之前配置的超時是5秒,來刷新一下https://blog.51cto.com/zero01/2173377https://www.jianshu.com/p/2dda8ac85a27可以的http://localhost:8010/getProductInfoList?number=1就是正常的,其實大家忽略了一個點,我改成1秒了hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000我現(xiàn)在改成1秒,我改成1秒,這邊超時是兩秒,那這里是要出現(xiàn)服務(wù)降級才對,我們再來看一下,你看為什么沒有出現(xiàn)服務(wù)降級呢,所以我們這個配置壓根就沒有生效,什么原因呢,我們沒有加注解,大家千萬要注意這一點@HystrixCommand @GetMapping("/getProductInfoList") public String getProductInfoList(@RequestParam("number") Integer number){if(number % 2 == 0){return "success";}RestTemplate restTemplate = new RestTemplate();return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class);}如果要做到服務(wù)降級的話,第一步一定要記得加上這個注解,不然是沒用的,再來刷新http://localhost:8010/getProductInfoList?number=1現(xiàn)在就出現(xiàn)太擁擠了,說明配置已經(jīng)生效,配置的是一秒,大家看到我這里配置的是defaulthystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000默認(rèn)的就是全局的作用,那如果我想單獨為這個方法設(shè)置一個超時時間呢,怎么做呢,大家可以看到這里有一個commandKey/*** Hystrix command key.* <p/>* default => the name of annotated method. for example:* <code>* ...* @HystrixCommand* public User getUserById(...)* ...* the command name will be: 'getUserById'* </code>** @return command key*/ String commandKey() default "";commandKey這是你自己可以設(shè)置的一個值,那默認(rèn)如果沒設(shè)置是什么呢,他這里注釋已經(jīng)寫得非常清楚了,可以看一下,假設(shè)你這個方法是這么來寫的,那默認(rèn)的就是方法名,其實大家可以看一下他的注解,看一下他的源碼,還是很有用的,那既然他的CommandKey是這個,方法是這個getProductInfoList,我為這個方法單獨設(shè)置一個超時時間hystrix:command:getProductInfoList:execution:isolation:thread:timeoutInMilliseconds: 3000hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000注意對齊,default和我自定義的,同一級,這邊設(shè)置5秒,再試一下,再來訪問,看這個時候就可以訪問到了 server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=order eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}spring.rabbitmq.host=59.110.158.145 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.port=5672spring.cloud.stream.bindings.myMessage.group=order spring.cloud.stream.bindings.myMessage.content-type=application/jsonhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000 package com.learn.controller;import java.util.Arrays;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@RestController @DefaultProperties(defaultFallback = "defaultFallback") public class HystrixController {//@HystrixCommand(fallbackMethod = "fallback")//2、超時設(shè)置 // @HystrixCommand(commandProperties = { // @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") //超時時間設(shè)置為3秒 // }) // 3. // @HystrixCommand(commandProperties = { // @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//設(shè)置熔斷 // @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), // @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") // })@HystrixCommand@GetMapping("/getProductInfoList")public String getProductInfoList(@RequestParam("number") Integer number){ // public String getProductInfoList(){if(number % 2 == 0){return "success";}RestTemplate restTemplate = new RestTemplate();return restTemplate.postForObject("http://127.0.0.1:7900/product/listForOrder", Arrays.asList("157875196366160022"),String.class);}private String fallback(){return "太擁擠了,請稍后再試~~";}private String defaultFallback(){return "默認(rèn)提示:太擁擠了,請稍后再試~~";} }

?

總結(jié)

以上是生活随笔為你收集整理的使用配置项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。