-
2018-05-15 09:47:061、这个主要是由作用范围决定的。在方法内部定义的内部类,只能在方法的内部,也就是定义该类的区域内使用。
这样的结果就是,在方法之外,并不能访问到该内部类,也即不需要private等访问控制符修饰,因为他本身就是在内部使用的,外部不能访问
2、一个类中,一个static变量只会有一个内存空间,虽然有多个类实例,但这些类实例中的这个static变量会共享同一个内存空间。所以声明为static的变量实质上就是全局变量。所以static不能修饰局部变量。
此外,局部变量是存放在栈中的,程序运行完立即释放。它只能在定义它的方法内部使用。所以不用static修饰符。更多相关内容 -
Feign的各种超时时间(含局部方法设置超时案例)
2020-04-05 19:20:541000 # 局部设置超时: hystrix.command..execution.isolation.thread.timeoutInMilliseconds: 1000 2.3设置局部方法超时成功案例: 需要对下图两个方法局部设置超时时间为10秒, 方法一、配置如下: # 禁用Hystrix...关于Feign的超时记录:
在
Spring Cloud
微服务架构中,大部分公司都是利用Open Feign
进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果是业务比较复杂,服务要进行比较繁杂的业务计算,那后台很有可能会出现Read Timeout
这个异常。1、关于hystrix的熔断超时
如果
Feign
开启了熔断,必须要重新设置熔断超时的时间,因为默认的熔断超时时间太短了,只有1秒,这容易导致业务服务的调用还没完成然后超时就被熔断了。如何配置熔断超时:
-
#Feign如何开启熔断
-
feign.hystrix.enabled=true
-
#是否开始超时熔断,如果为false,则熔断机制只在服务不可用时开启(spring-cloud-starter-openfeign中的HystrixCommandProperties默认为true)
-
hystrix.command.default.execution.timeout.enabled=true
-
#设置超时熔断时间(spring-cloud-starter-openfeign中的HystrixCommandProperties默认为1000毫秒)
-
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
注意:关于
hystrix
在application.properties
配置是没提示的,但是HystrixCommandProperties
是会获取的。-
// 构造函数
-
protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder, String propertyPrefix) {
-
// .... 省略很多其他配置
-
// propertyPrefix:hystrix,key:default
-
this.executionTimeoutInMilliseconds = getProperty(propertyPrefix, key, "execution.isolation.thread.timeoutInMilliseconds", builder.getExecutionIsolationThreadTimeoutInMilliseconds(), default_executionTimeoutInMilliseconds);
-
}
-
// 具体获取属性的方法
-
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
-
return HystrixPropertiesChainedProperty.forString().add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue).add(propertyPrefix + ".command.default." + instanceProperty, defaultValue).build();
-
}
2、Feign局部设置超时间
spring-cloud-dependencies Dalston版本之后,默认Feign对Hystrix的支持默认是关闭的,需要手动开启。
feign.hystrix.enabled=true
开启hystrix,可以选择关闭熔断或超时。
2.1关闭熔断:-
# 全局关闭熔断:
-
hystrix.command.default.circuitBreaker.enabled: false
-
# 局部关闭熔断:
-
hystrix.command.<HystrixCommandKey>.circuitBreaker.enabled: false
2.2设置超时:
-
# 全局设置超时:
-
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000
-
# 局部设置超时:
-
hystrix.command.<HystrixCommandKey>.execution.isolation.thread.timeoutInMilliseconds: 1000
2.3设置局部方法超时成功案例:
需要对下图两个方法局部设置超时时间为10秒,
方法一、配置如下:
-
# 禁用Hystrix超时
-
hystrix.threadpool.default.coreSize = 10
-
hystrix.command.default.fallback.enabled = true
-
hystrix.command.default.execution.timeout.enabled= true
-
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 3000
-
hystrix.command.MemberStaffFeign#getExcelDataByDoctor(String,Integer,Integer).execution.isolation.thread.timeoutInMilliseconds=10000
-
hystrix.command.MemberStaffFeign#getExcelDataByTeam(String,Integer,Integer).execution.isolation.thread.timeoutInMilliseconds=10000
@SpringBootApplication启动类中添加@EnableCircuitBreaker注解
-
@EnableFeignClients
-
@EnableEurekaClient
-
@EnableDiscoveryClient
-
@EnableApolloConfig
-
@EnableCircuitBreaker
-
@SpringBootApplication
-
public class BusinessSystemApplication {
-
public static void main(String[] args) {
-
ConfigurableApplicationContext app = new SpringApplicationBuilder(BusinessSystemApplication.class).run(args);
-
System.out.println(app.getEnvironment().getProperty("spring.application.name") + "服务启动完毕...");
-
}
-
}
2.4关闭超时
-
# 全局关闭:
-
hystrix.command.default.execution.timeout.enabled: false
-
# 局部关闭:
-
hystrix.command.<HystrixCommandKey>.execution.timeout.enabled: false
3、关于Ribbon超时。
Feign
调用默认是使用Ribbon
进行负载均衡的,所以我们还需要了解关于Ribbon
的超时。①、Feign的调用链路
看一下Feign的请求是否有使用Ribbon的超时时间,而且是如何读取Ribbon的超时时间的?
(1)、org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#execute
(2)、com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(S, com.netflix.client.config.IClientConfig)
(3)、org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory#create
-
创建Client,这里会判断对应ClientName的链接Client是否创建过,如果创建过复用之前的Client;
-
如果不存在则创建一个并且放入cache缓存。
-
public FeignLoadBalancer create(String clientName) {
-
FeignLoadBalancer client = this.cache.get(clientName);
-
if(client != null) {
-
return client;
-
}
-
IClientConfig config = this.factory.getClientConfig(clientName);
-
ILoadBalancer lb = this.factory.getLoadBalancer(clientName);
-
ServerIntrospector serverIntrospector = this.factory.getInstance(clientName, ServerIntrospector.class);
-
// 判断是否有重试
-
client = loadBalancedRetryFactory != null ? new RetryableFeignLoadBalancer(lb, config, serverIntrospector,
-
loadBalancedRetryFactory) : new FeignLoadBalancer(lb, config, serverIntrospector);
-
this.cache.put(clientName, client);
-
return client;
-
}
(4)、com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(S, com.netflix.client.config.IClientConfig)
负载均衡器抽象类
(5)、org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer#execute
-
Feign的负载均衡器实现类。到这里我们可以看到,连接超时和读超时的配置都在这里:
-
如果application.properties配置文件中的超时时间不为空,则使用配置的超时时间。
-
如果为空则使用默认值,而从FeignLoadBalancer的构造函数可以看到,默认值也是取的RibbonProperties的默认超时时间。
-
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
-
throws IOException {
-
Request.Options options;
-
// 设置超时时间。,如果orride的配置为空,则用默认值
-
if (configOverride != null) {
-
RibbonProperties override = RibbonProperties.from(configOverride);
-
options = new Request.Options(
-
override.connectTimeout(this.connectTimeout),
-
override.readTimeout(this.readTimeout));
-
}
-
else {
-
options = new Request.Options(this.connectTimeout, this.readTimeout);
-
}
-
// 发起请求
-
Response response = request.client().execute(request.toRequest(), options);
-
return new RibbonResponse(request.getUri(), response);
-
}
-
// 构造函数
-
public FeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig, ServerIntrospector serverIntrospector) {
-
super(lb, clientConfig);
-
this.setRetryHandler(RetryHandler.DEFAULT);
-
this.clientConfig = clientConfig;
-
this.ribbon = RibbonProperties.from(clientConfig);
-
RibbonProperties ribbon = this.ribbon;
-
this.connectTimeout = ribbon.getConnectTimeout();
-
this.readTimeout = ribbon.getReadTimeout();
-
this.serverIntrospector = serverIntrospector;
-
}
②、Ribbon的默认超时时间
在
RibbonClientConfiguration
中:-
public static final int DEFAULT_CONNECT_TIMEOUT = 1000;
-
public static final int DEFAULT_READ_TIMEOUT = 1000;
③、如何自定义Ribbon超时时间
首先,
RibbonProperties
的超时时间的读取的源码如下:-
public Integer getConnectTimeout() {
-
return (Integer)this.get(CommonClientConfigKey.ConnectTimeout);
-
}
-
public Integer getReadTimeout() {
-
return (Integer)this.get(CommonClientConfigKey.ReadTimeout);
-
}
然后,可以在
CommonClientConfigKey
中可以看到两个超时时间的名称:-
// ConnectTimeout:
-
public static final IClientConfigKey<Integer> ConnectTimeout = new CommonClientConfigKey<Integer>("ConnectTimeout") {};
-
// ReadTimeout:
-
public static final IClientConfigKey<Integer> ReadTimeout = new CommonClientConfigKey<Integer>("ReadTimeout") {};
然后,在
IClientConfig
的默认实现类:DefaultClientConfigImpl
中,可以发现Ribbon
配置的前缀public static final String DEFAULT_PROPERTY_NAME_SPACE = "ribbon";
所以,最后
Ribbon
该这么配置超时时间:-
ribbon.ConnectTimeout=5000
-
ribbon.ReadTimeout=5000
总结
1.如何配置好
Hystrix
和Ribbon
的超时时间呢?其实是有套路的,因为
Feign
的请求:其实是Hystrix
+Ribbon
。Hystrix
在最外层,然后再到Ribbon
,最后里面的是http
请求。所以说。Hystrix
的熔断时间必须大于Ribbon
的 (ConnectTimeout
+ReadTimeout
)。而如果Ribbon
开启了重试机制,还需要乘以对应的重试次数,保证在Ribbon
里的请求还没结束时,Hystrix
的熔断时间不会超时。 -
-
一起学JAVA 方法 局部变量 成员变量
2021-04-01 23:59:301 变量 1.1 概念 可以改变的数,称为变量。在Java语言中,所有的变量在使用前必须声明。...作用域:也就是方法里或者局部代码块中,方法运行完内存就释放了 2.3 成员变量 位置:定义在类里方法外 注意:不用1 变量
1.1 概念
可以改变的数,称为变量。在Java语言中,所有的变量在使用前必须声明。
一般通过“变量类型 变量名 = 变量值 ;”这三部分来描述一个变量。如:int a = 3 ;
变量的使用原则:就近原则,即尽量控制变量的使用范围到最小1.2 局部变量
位置:定义在方法里或者方法的声明上
注意:必须手动初始化来分配内存.如:int i = 5;或者int i; i = 5;
生命周期:随着方法的调用而存在,方法运行完毕就释放了1.3 成员变量
位置:定义在类里方法外
注意:不用初始化,也会自动被初始化成默认值
生命周期:整个类中,类消失了,成员变量才会释放
1.4 练习:变量的默认值测试
创建包: cn.tedu.basic
创建类: TestVariable1.javapackage cn.tedu.design; /*本类用于测试各种类型变量的默认值*/ public class TestVariable1 { static String name; static byte b;//整数类型默认值都是0 static short c; static int d; static long e; static float f;//小数类型的默认值是0.0 static double g; static char j;//char类型的默认值是\u0000 static boolean h;//boolean类型的默认值是false public static void main(String[] args) { System.out.println(name);//null,引用类型的默认值 System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); System.out.println(f); System.out.println(g); System.out.println(h); System.out.println(j); System.out.println(h); } }
1.5 练习:局部变量与成员变量测试
创建包: cn.tedu.basic
创建类: TestVariable2.javapackage cn.tedu.oop; /**本类用于测试变量的使用*/ public class TestVariable2 { //2.定义成员变量: //1)位置:类里方法外 //2)无需手动初始化,会自动赋予对应类型的默认值 //3)作用域:在整个类中生效,类消失,变量才会消失 static int count; //3.变量有一个使用的原则:就近原则 static int sum = 200; public static void main(String[] args) { //1.定义局部变量: //1)位置:在方法里/局部代码块里 //2)必须手动初始化 //3)作用域:在方法/局部代码块中,对应的代码执行完局部变量就被释放 int sum = 100;//定义在方法中的局部变量sum System.out.println(sum);//变量的就近原则:使用的都是自己附近的变量,100 System.out.println(count); for (int i = 0; i < 10; i++) {//局部变量i只能在循环中使用 System.out.println(i); } //System.out.println(i);//报错:无法引用变量i:i cannot be resolved to a variable } }
2 方法
2.1 概述
被命名的代码块,方法可以含参数可以不含参数,可以提高代码的复用性。
2.2 方法定义的格式
2.3 方法调用顺序图
顺序执行代码,调用指定方法,执行完毕,返回调用位置
2.4 练习:测试方法的调用顺序/参数/返回值
创建包:cn.tedu.method
创建类:TestMethod .javapackage cn.tedu.method; /**本类用于测试方法*/ public class TestMethod { //1.创建程序的入口函数main() public static void main(String[] args) { System.out.println(1); /**2.我们通过方法名+参数列表的方式来调用方法的功能*/ method1();//调用method1() System.out.println(2); method2(3);//调用method2() int result = method3(1,2);//调用method3() System.out.println(result); } /**3.如果方法想要返回值,必须修改返回值类型 * 并且return对应类型的结果 * 如果方法的返回值类型是void,不允许有返回值 * */ /*本方法用来测试方法的返回值类型*/ public static int method3(int i, int j) { /**4.通过return关键字将方法结果返回到调用位置*/ return i+j; } /**1.方法的修饰符 方法的返回值类型 方法名(方法参数){方法体}*/ /*method1()想测试方法的调用顺序*/ public static void method1() { System.out.println(5); System.out.println(6); System.out.println(7); } /*本方法用来测试方法的参数,参数的位置在小括号里*/ public static void method2(int a) { System.out.println("海绵宝宝今年:"+ a +"岁啦~"); } }
2.5 方法的重载
方法的重载是指在一个类中定义多个同名的方法,但是每个方法的参数列表不同(也就是指参数的个数和类型不同),程序在调用方法时,可以通过传递给他们的不同个数和类型的参数来决定具体调用哪个方法.
2.6 练习:测试方法的重载
创建包: cn.tedu.method
创建类: TestMethodOverload.javapackage cn.tedu.method; /**本类用于测试方法的重载*/ public class TestMethodOverload { public static void main(String[] args) { /**1.我们根据方法名+参数列表确定具体调用哪个方法*/ /**2.方法的重载: * 在同一个类中,存在方法名相同,但参数列表不同的方法 * 如果在同类中,同名方法的参数个数不同,一定构成重载 * 如果在同类中,同名方法的参数个数相同, * 需要查看对应位置上参数的类型,而不是参数名,与参数名无关 * (int a,String b)与(int b,String a)--不构成重载 * (int a,String b)与(String a,int b)--构成重载 * */ //2.调用method() method(); //4.调用method(int) method(666); //6.调用method(int,String) method(888,"泡泡"); } //1.创建一个无参的method() public static void method() { System.out.println("哈哈哈哈我没参数"); } //3.创建一个method(int n) public static void method(int n) { System.out.println("哈哈哈哈我的参数是:"+n); } //5.创建一个method(int n,String s) public static void method(int a,String b) { System.out.println(b+"今晚要干他"+a+"碗大米饭"); } public static void method(String a,int b) { System.out.println(b+"今晚要干他"+a+"碗大米饭"); } }
3 拓展 成员变量与局部变量的区别
-
jdk1.8之后内部类调用局部方法不用final
2019-07-31 18:03:59局部内部类如果要去访问局部变量,那么局部变量必须声明为final类型。 具体可以看下java内部类介绍 也就是 public class demo { public static void main(String[] args) { doSomething(); } private static ...我们都知道java的内部类
局部内部类如果要去访问局部变量,那么局部变量必须声明为final类型。
具体可以看下java内部类介绍
也就是public class demo { public static void main(String[] args) { doSomething(); } private static void doSomething() { final String str1 = "Hello"; String str2 = "World!"; // 创建一个方法里的局部内部类 class Test { public void out() { System.out.println(str1); System.out.println(str2); } } Test test = new Test(); test.out(); } }
这个方法在运行到 System.out.println(str2); 时候会报错
由于str2没有声明为final,编译抛出异常:Cannot refer to the non-final local variable str2 defined in an enclosing scope但是现在运行的时候却发现成功了
感到十分的奇怪 于是查了下资料
得知 JDK1.8之后匿名内部类访问方法中的局部变量不用加final修饰具体可以文档参考https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html#accessing-members-of-an-enclosing-class
-
局部变量是否线程安全
2022-03-12 10:48:55了解过JVM的都知道,堆和方法区(JDK1.8后叫元空间)是线程共享的 ...局部 变量表 操作数栈 动态链接【方法的符号引用,在这里我们可以讨论虚方法(在运行时确定方法,把符号引用转为直接引用)和非虚方 -
基于局部Hough变换的机场跑道检测方法.docx
2022-06-18 04:58:52基于局部Hough变换的机场跑道检测方法.docx基于局部Hough变换的机场跑道检测方法.docx基于局部Hough变换的机场跑道检测方法.docx基于局部Hough变换的机场跑道检测方法.docx基于局部Hough变换的机场跑道检测方法.docx... -
基于局部Hough变换的机场跑道检测方法.pdf
2022-06-18 03:29:32基于局部Hough变换的机场跑道检测方法.pdf基于局部Hough变换的机场跑道检测方法.pdf基于局部Hough变换的机场跑道检测方法.pdf基于局部Hough变换的机场跑道检测方法.pdf基于局部Hough变换的机场跑道检测方法.pdf基于... -
C#7.0新特性-局部函数
2019-06-16 10:44:04局部函数就是函数中套函数 例如: static void Main(string[] args) { int Result = Add(5, 7) + subtract(6,2); Console.WriteLine("结果:"+Result); Console.ReadKey(); ... -
java局部变量,成员变量在堆和栈中的存储
2020-09-08 16:43:59对于局部变量,如果是基本类型,会把值直接存储在栈;如果是引用类型,比如String s = new String("william");会把其对象存储在堆,而把这个对象的引用(指针)存储在栈。 再如 String s1 = new String(“william”)... -
java方法 成员变量 局部变量概述
2017-12-23 20:37:581. 方法 1.1 方法概述 java中方法就是用来解决某件事或者实现某个功能的办法。如果需要定义一个方法,则只能在类里面定义,不能不能独立定义一个方法。一旦将一个方法定义在某个类的类体内,如果这个方法使用了... -
栈帧中局部变量表,操作数栈,动态链接,方法出口的理解
2020-07-19 10:03:36栈帧存储了方法的局部变量表、操作数栈、动态连接和方法返回地址等信息。每一个方法从调用至执行完成的过程,都对应着一个栈帧在虚拟机栈里从入栈到出栈的过程。 一个线程中方法的调用链可能会很长,很多方法都同时... -
Vue刷新局部数据的方法
2022-01-25 17:02:58如果使用原始方法location.reload()则会造成闪烁,于是我们可以使用以下方法。 在App.vue定义reload方法 <template> <div id="app"> <router-view v-if="isRouterAlive"> -
方法中的局部变量
2017-03-28 16:27:162.当调用方法change的时候,change方法栈帧入栈,局部变量a在栈帧上操作a+=5 3.当change方法结束的时候,栈帧退栈,打印a的值,a仍然是1。 因为java中,基本类型就是存储在虚拟栈中的。当然如果是全局... -
核平滑方法——局部多项式回归
2020-05-31 14:31:251. 核平滑方法 代码实现 2. 局部多项式核回归 2.1 加权最小二乘法(Weighted least squares) 2.2 局部多项式核回归(Local polynomial kernel regression) 代码实现 -
匿名内部类访问外部类方法中的局部变量
2019-09-04 16:13:16重点!重点!重!点 匿名内部类不能访问外部类方法中的局部变量,除非变量被声明为final类型 这里所说的“匿名内部类”...局部变量的生命周期:当该方法被调用时,该方法中的局部变量在栈中被创建,当方法调用结束... -
java调用局部内部类中的方法
2019-08-09 12:21:26局部内部类中方法的调用 -
用java的反射机制怎么获取一个类中里面方法中局部变量
2021-03-09 21:22:31public void setName 类的方法有:public void setAge 类的方法有:public int getAge 类的方法有:public final void wait 类的方法有:public final void wait 类的方法有:public final native ... -
成员变量和局部变量的初始值和赋值操作问题
2019-09-14 15:12:47public class demo{ int x ; x = 1;//这句会报错 } 上边是两个语句,第一个,声明一个类的...(类里面只能放成员变量和方法吧,赋值是个操作) public class demo{ int x = 1; //这条语句是声明成员变量的同时并... -
页面局部刷新三种方法
2019-09-21 16:00:38页面局部刷新 1.使用ajax实现小部分的内容局部刷新 $.ajax({ url:“http://localhost:8080/intoHomepage.do”, type:“post”, data:{name:“mydata”}, success:function (result) { if(result){ //对后端返回的... -
如何调用局部内部类中的方法
2018-04-01 10:12:57内部类:将类写在其他类的内部,可以写在其他类的成员位置和局部位置局部内部类:将类写在其他类的局部方法中例如public class outer { public void out(){ int i=2; class Inner{ int a=1; public void ... -
PointWeb:一种增强点云处理中局部邻域特征提取的方法
2022-01-07 21:27:01PointWeb:一种增强点云处理中局部邻域特征提取的方法 相关网站 pointweb:Enhancing Local Neighborhood Features for Point Cloud Processing 这是2019年发布在cvpr上的文章 论文地址 :... -
jQuery实现局部刷新的几种方式
2017-04-09 15:17:55使用jquery实现局部刷新,我一般会使用两种方法。 一种是真刷新,从服务器端获取刷新后的结果,并输出到页面。 另一种是假刷新,得到参数更改的确认之后,并不从服务端得到数据,而是直接使用脚本,将现有页面的... -
matlab画一个局部放大的图中图
2017-04-19 10:10:00第一种:magnify是个动态放大镜,固化后可以用tools>edit plot移动小图,能选取多个局部图,这个方法不错 用法:打开figure图,输入magnify,左键动态选取查看,ctrl+左键固化,也可右键固化,‘’缩放方法范围,... -
方法中定义的局部变量是否为线程安全的?
2021-05-12 22:50:36方法中定义的局部变量是否为线程安全的? 具体问题,具体分析。 比如我们就拿StringBuilder来分析吧,首先你需要明白:StringBuilder和StringBuffer都是可变的,但是StringBuffer是线程安全的,而StringBuilder是... -
局部自适应阈值分割方法
2018-03-22 09:34:47Local_Yanowitz 由于光照的影响,图像的灰度可能是不均匀分布的,此时...Yanowitz提出了一种局部阈值分割方法。结合边缘和灰度信息找到阈值表面(treshhold surface)。在阈值表面上的就是目标。 算法的主要... -
前端页面局部(全局)刷新方法
2019-07-11 15:42:26前端页面局部(全局)刷新方法 JS / Jquery 刷新方法: //div的局部刷新 <div class="dl"></div> $(".dl").load(location.href+" .dl"); //全页面的刷新方法 window.location.reload()刷新当前... -
RecyclerView局部刷新的方法。
2018-07-18 22:03:33之前这种RecyclerView的复用缓存我都是放在list集合中管理,每次改变一个item的状态时候都会将集合重新修改然后...所以就在查找资料关于recyclerview局部刷新的。一般就两种做法:第一种就是通过recyc... -
vue局部刷新页面的三种方法
2020-02-20 16:00:14方法一: location.reload(); 方法二: this.$router.go(0); 方法三: provide() 与 inject 结合; 在App.vue中 <template> <router-view v-if="isRouterAlive"/> </template> <script> ...