一些难以排查的错

1.非注册中心项目启动,莫名其妙报错

Request execution failed with message: java.net.ConnectException: Connection refused: connect
DiscoveryClient_UNKNOWN/localhost - was unable to refresh its cache! status = Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server


网上很多解决方案如下:

//表示是否将自己注册到Eureka Server,默认为true。
eureka.client.register-with-eureka=false
// 表示是否从Eureka Server获取注册信息,默认为true。
eureka.client.fetch-registry=false


一搜一大把五花八门的,还带各种解释,刚学习嘛,也见怪不怪,少写了的,写错了的,
以上配置完后依旧报该错误,不妨看看pom下的配置。
许多人都是idea下spring Initializr构建的springcloud项目,方便是方便了,可是有时候带来的一些错误就让人很头疼了。构建的st项目会自带maven插件如下

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

该插件的引用就会导致以上异常。

2.服务注册不到注册中心

在座大家搭建的微服务学习案例可能是根据一些博客,案例来学习,借鉴的,刚开始学习嘛,借鉴也没有错,但是不能完全依赖。每个人学习都有每个人的理解,以及应用场景,所以从网上看到的大部分案例,写法都是五花八门的,因为你不了解,可能这里借鉴借鉴,那里借鉴借鉴,最终一运行,报错了,这个时候吧,就完全不知道哪里出了问题,然后就是一顿百度,谷歌,这里改哪里重写,最后就形成了一个整合的乱七八糟的框架。扯远了,我碰到的这个问题,主要是jar包引用的问题,虽然现在有很多封装好的现成的starter可以使用,但是也不要乱引用,有时候莫名其妙报个错,够你找的。多看看官方文档,光看案例,吃现成的,是不能有太大提升的。
我的用法很简单,不使用任何多余的,大家可以借鉴借鉴,但还是要自己摸索,献丑了。。。

1.注册中心pom

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

2.服务提供者pom

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
</dependencies>

3.服务调用者pom

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

此处配置为最干净的配置,不掺杂其余组件,仅包括注册中心,及服务提供,服务调用。

3.使用RestTemplate找不到服务

错误描述:使用RestTemplate调用远程服务时,一般都是根据spring.application.name配置的服务名称进行调用。明明服务存在,但是就是访问超时,即,获取不到调用服务,不妨加上 @LoadBalanced试试。

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        //防止超时
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        //建立连接所用的时间 5s
        simpleClientHttpRequestFactory.setConnectTimeout(5000);
        //服务器读取可用资源的时间 10s
        simpleClientHttpRequestFactory.setReadTimeout(600000);
        return new RestTemplate(simpleClientHttpRequestFactory);
    }

4.链接数据库的项目启动报错找不到spring.datasource.url

报错内动大致是找不到url,找不到对应数据库驱动,明明都引入依赖了,为何呢?

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class
Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

不妨看看是都引入了spring-boot-devtools热部署插件,有时候同样会影响造成找错误:Cannot execute request on any known server

谢谢大家关注,点个赞呗~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。