0%

对于 springcloud config 的配置

如果eureka的端口不是默认的端口,需要把端口写回配置文件,因为项目启动是先去找eureka,再获取配置

1
2
3
4
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/

使用springcloud bus的bus-refresh接口需要springboot暴露该接口

需要在controller加@RefreshScope

1
2
3
4
5
management:
endpoints:
web:
exposure:
include: "*"

添加webhook需要添加如下依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>

springcloud stream 同一个服务input信道和output信道不能一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public interface StreamClient {

String INPUT = "input";

String OUTPUT = "output";

@Input(StreamClient.INPUT)
SubscribableChannel input();

@Output(StreamClient.OUTPUT)
MessageChannel output();

/*@Input(StreamClient.INPUT2)
SubscribableChannel input2();

@Output(StreamClient.INPUT2)
MessageChannel output2();*/
}

Gson转list对象的方法

1
List<ProductInfo> productInfoList = new Gson().fromJson(message,new TypeToken<List<ProductInfo>>(){}.getType());

对于springcloud zuul默认过滤了cookie

zuul:

routes:
myProduct:
path: /myProduct/**
serviceId: product
sensitiveHeaders:

使用sensitiveHeaders设置为空即可不过滤cookie

对于springcloud hystrix dashboard

进入的时候,如果你是http,请不要复制他那个请求,他那个请头是https,改成http,并且把这段代码写入即可

1
2
3
4
5
6
7
8
9
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}

在springcloud的多模块打包问题中

mvn install 死活打包不了依赖了其他模块的模块,后来百度才发现,是spring-boot-maven-plugin这个玩意,打包的jar包只能运行,不能被依赖

所以要被依赖的jar包请把这个plugin改成下面这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
<configuration>
<classifier>execute</classifier>
</configuration>
</plugin>
</plugins>
</build>

题一

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class N136 {

public int singleNumber(int[] nums) {
int ans=nums[0];
if(nums.length>1){
for(int i=1;i<nums.length;i++){
ans=ans^nums[i];
}
}
return ans;

}
}

https://leetcode-cn.com/problems/single-number/

题二

给你一个整数数组 arr

现需要从数组中取三个下标 ijk ,其中 (0 <= i < j <= k < arr.length)

ab 定义如下:

  • a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
  • b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]

注意:^ 表示 按位异或 操作。

请返回能够令 a == b 成立的三元组 (i, j , k) 的数目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static int countTriplets(int[] arr) {

int res=0;

if(arr.length<2){
return 0;
}
for(int i=0;i<arr.length;i++){
int temp=arr[i];
for(int j=i+1;j<arr.length;j++){
temp=temp^arr[j];
if(temp==0){
res=res+j-i;
}
}

}
return res;

}

https://leetcode-cn.com/classic/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/description/?tdsourcetag=s_pctim_aiomsg

需要在application.yml里面连接数据库时添加运行执行多条sql语句

&allowMultiQueries=true

1
2
3
4
5
6
7
8
9
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
redis:
host: 127.0.0.1
port: 6379
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<update id="updateByListPrimaryKey" parameterType="java.util.List">
<foreach collection="productList" index="index" item="item" separator=";" >
update mall_product
<set>
<if test="item.categoryId != null">
category_id = #{item.categoryId,jdbcType=INTEGER},
</if>
<if test="item.name != null">
name = #{item.name,jdbcType=VARCHAR},
</if>
<if test="item.subtitle != null">
subtitle = #{item.subtitle,jdbcType=VARCHAR},
</if>
<if test="item.mainImage != null">
main_image = #{item.mainImage,jdbcType=VARCHAR},
</if>
<if test="item.subImages != null">
sub_images = #{item.subImages,jdbcType=VARCHAR},
</if>
<if test="item.detail != null">
detail = #{item.detail,jdbcType=VARCHAR},
</if>
<if test="item.price != null">
price = #{item.price,jdbcType=DECIMAL},
</if>
<if test="item.stock != null">
stock = #{item.stock,jdbcType=INTEGER},
</if>
<if test="item.status != null">
status = #{item.status,jdbcType=INTEGER},
</if>

</set>
where id = #{item.id,jdbcType=INTEGER}
</foreach>
</update>

设置session的过期时间

1
2
3
4
server:
servlet:
session:
timeout: PT5H

其中T是天和小时的分界符,不区分大小写,时间必须按顺序

比如说 P2dt5h3m30s

就是两天五小时三分钟三十秒

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@PostMapping("/user/login")
public ResponseVO<User> login(@Valid @RequestBody UserLoginForm userLoginForm,
BindingResult bindingResult,
HttpServletRequest httpServletRequest){
if(bindingResult.hasErrors()){
return ResponseVO.error(ResponseEnum.PARAM_ERROR,bindingResult);

}

ResponseVO<User> responseVO = userService.login(userLoginForm.getUsername(), userLoginForm.getPassword());

//设置session
HttpSession session = httpServletRequest.getSession();
session.setAttribute(MallConst.CURRENT_USER,responseVO.getData());

return responseVO;

}

还有另外一种获取方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@PostMapping("/user/login")
public ResponseVO<User> login(@Valid @RequestBody UserLoginForm userLoginForm,
BindingResult bindingResult,
HttpSession session){
if(bindingResult.hasErrors()){
return ResponseVO.error(ResponseEnum.PARAM_ERROR,bindingResult);

}

ResponseVO<User> responseVO = userService.login(userLoginForm.getUsername(), userLoginForm.getPassword());

//设置session
//HttpSession session = httpServletRequest.getSession();
session.setAttribute(MallConst.CURRENT_USER,responseVO.getData());

return responseVO;

}