1.非注冊(cè)中心項(xiàng)目啟動(dòng)熬尺,莫名其妙報(bào)錯(cuò)
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
網(wǎng)上很多解決方案如下:
//表示是否將自己注冊(cè)到Eureka Server摸屠,默認(rèn)為true。
eureka.client.register-with-eureka=false
// 表示是否從Eureka Server獲取注冊(cè)信息粱哼,默認(rèn)為true季二。
eureka.client.fetch-registry=false
一搜一大把五花八門的,還帶各種解釋揭措,剛學(xué)習(xí)嘛胯舷,也見怪不怪,少寫了的绊含,寫錯(cuò)了的桑嘶,
以上配置完后依舊報(bào)該錯(cuò)誤,不妨看看pom下的配置躬充。
許多人都是idea下spring Initializr構(gòu)建的springcloud項(xiàng)目逃顶,方便是方便了讨便,可是有時(shí)候帶來的一些錯(cuò)誤就讓人很頭疼了。構(gòu)建的st項(xiàng)目會(huì)自帶maven插件如下
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
該插件的引用就會(huì)導(dǎo)致以上異常以政。
2.服務(wù)注冊(cè)不到注冊(cè)中心
在座大家搭建的微服務(wù)學(xué)習(xí)案例可能是根據(jù)一些博客霸褒,案例來學(xué)習(xí),借鑒的盈蛮,剛開始學(xué)習(xí)嘛傲霸,借鑒也沒有錯(cuò),但是不能完全依賴眉反。每個(gè)人學(xué)習(xí)都有每個(gè)人的理解,以及應(yīng)用場景穆役,所以從網(wǎng)上看到的大部分案例寸五,寫法都是五花八門的何暮,因?yàn)槟悴涣私饬赡苓@里借鑒借鑒,那里借鑒借鑒伞租,最終一運(yùn)行淹接,報(bào)錯(cuò)了十性,這個(gè)時(shí)候吧,就完全不知道哪里出了問題塑悼,然后就是一頓百度劲适,谷歌,這里改哪里重寫厢蒜,最后就形成了一個(gè)整合的亂七八糟的框架霞势。扯遠(yuǎn)了,我碰到的這個(gè)問題斑鸦,主要是jar包引用的問題愕贡,雖然現(xiàn)在有很多封裝好的現(xiàn)成的starter可以使用,但是也不要亂引用巷屿,有時(shí)候莫名其妙報(bào)個(gè)錯(cuò)固以,夠你找的。多看看官方文檔嘱巾,光看案例憨琳,吃現(xiàn)成的,是不能有太大提升的浓冒。
我的用法很簡單栽渴,不使用任何多余的,大家可以借鑒借鑒稳懒,但還是要自己摸索闲擦,獻(xiàn)丑了慢味。。墅冷。
1.注冊(cè)中心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.服務(wù)提供者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.服務(wù)調(diào)用者pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
此處配置為最干凈的配置纯路,不摻雜其余組件,僅包括注冊(cè)中心寞忿,及服務(wù)提供驰唬,服務(wù)調(diào)用。
3.使用RestTemplate找不到服務(wù)
錯(cuò)誤描述:使用RestTemplate調(diào)用遠(yuǎn)程服務(wù)時(shí)腔彰,一般都是根據(jù)spring.application.name配置的服務(wù)名稱進(jìn)行調(diào)用叫编。明明服務(wù)存在,但是就是訪問超時(shí)霹抛,即搓逾,獲取不到調(diào)用服務(wù),不妨加上 @LoadBalanced試試杯拐。
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
//防止超時(shí)
SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
//建立連接所用的時(shí)間 5s
simpleClientHttpRequestFactory.setConnectTimeout(5000);
//服務(wù)器讀取可用資源的時(shí)間 10s
simpleClientHttpRequestFactory.setReadTimeout(600000);
return new RestTemplate(simpleClientHttpRequestFactory);
}
4.鏈接數(shù)據(jù)庫的項(xiàng)目啟動(dòng)報(bào)錯(cuò)找不到spring.datasource.url
報(bào)錯(cuò)內(nèi)動(dòng)大致是找不到url霞篡,找不到對(duì)應(yīng)數(shù)據(jù)庫驅(qū)動(dòng),明明都引入依賴了端逼,為何呢朗兵?
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熱部署插件,有時(shí)候同樣會(huì)影響造成找錯(cuò)誤:Cannot execute request on any known server
謝謝大家關(guān)注顶滩,點(diǎn)個(gè)贊唄~