1、Eureka切换Nacos时发现两个注册中心的解决方法
Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:
- nacosRegistration: defined by method 'nacosRegistration' in class path resource [org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.class]
- eurekaRegistration: defined in BeanDefinition defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.class]
解决过程记录:
(1)在XXApplicayion启动类上添加注解
@SpringBootApplication(exclude = EurekaClientAutoConfiguration.class)
会报新的错误:
Description:
Parameter 0 of method discoverClient in org.springbootframework.cloud.netflix.eureka.EurekaDiscoverClientConfiguration required a bean of type 'com.netflix.discovery.EurekaClient'
Action:
Consider defining a bean of type 'com.netflix.discovery.EurekaClient' in your configuration.
(2)删除pom文件中相关eureka依赖
未生效
(3)重启项目
重启应用后发现启动类上的import引入的eureka相关引用未删除,删除后,启动成功。
2、新打开的项目没有maven工具栏或者maven工具栏消失
(1)双击shift-->在出现的搜索框中输入maven-->选择 + Add MavenProjects
(2)如果提示此项目没有相关maven文件,注意查看分支,切换到正确的分支,选择对了分支,右下角也会有提示 load maven project
3、单元测试中怎么mock类型为private static的方法
try (MockedStatic mocked = mockStatic(Foo.class)) {
mocked.when(Foo.ethod).thenReturn("bar");
}
4、postman报错:Error: connect ECONNREFUSED ip:端口
网上解决方案很多,但是!!!注意!!!首先检查是不是应用未起来的原因。
5、数据库相关
(1)sql报错
multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage
原因:查询到的结果数太多,超过了binlog最大缓存设置
解决方案:在sql语句后加上 limit 1000;
(2)连接报错:java.sql.SQLNonTransientConnectionException:Too many connections
原因:数据库连接数过多
解决方案:增加数据库连接数最大值(注意评估服务器配置,如果服务器配置过低,数据库连接数设置太大,会影响数据库性能)
#查询最大连接数
show variables like '%max_connections%';
#修改最大连接数
set global max_connections=500
6、写在yaml中的配置怎么读取
yaml/yml
autotest:
server:
url: http://localhost:8080
properties
autotest.server.url=http://localhost:8080
(1)基础用法
@Value("${autotest.server.url")
private String url;
(2) spring的 Environment 类
SpringBoot 可以使用 @Autowired 注解注入 Environment 对象
SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,使用只需要通过调用 Environment 对象的getProperty(String name) 方法即可获取
例如上面 yml 或 properties 中的值可以通过
@RestController
@RequestMapping("/testCon")
public class BookController {
@Autowired
private Environment env;
@GetMapping("/{id}")
public void gettest(@PathVariable Integer id){
System.out.println(env.getProperty("autotest.server.url"));
}
}
7、controller引入不了service报错
现象:controller层和serviceImpl中的xxxService都爆红
解决方案:(1)清缓存and重启
(2)检查 auto import设置
(3) maven clean
8、对象存储使用报错
com.amazonaws.services.s3.model.AmazonS3Exception: null (Service: Amazon S3; Status Code: 403;
原因:accessKey和secretKey不对
解决方案:更新accessKey和secretKey