錯(cuò)誤描述
如果你的cloud-demo項(xiàng)目當(dāng)輸入http://localhost:8080/order/101
,想查看訂單編號(hào)為101
的數(shù)據(jù),網(wǎng)頁前端顯示如下錯(cuò)誤
IDEA顯示錯(cuò)誤:
07-05 16:39:16:251 ERROR 10056 — [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for userservice] with root cause
java.lang.IllegalStateException: No instances available for userservice
…
下面還有一大堆
但是如果查詢用戶信息時(shí),并不會(huì)報(bào)錯(cuò)。比如http://localhost:8081/user/1
。瀏覽器返回
{
"id": 1,
"username": "柳巖",
"address": "湖南省衡陽市"
}
ps:如果瀏覽器不是json格式的數(shù)據(jù),chrome瀏覽器可以直接安裝插件JSONView。
問題解決:
如果你的service接口正確的添加了注解@Service
、Mapper接口正確的添加了注解@Mapper
、在OrderService中,要對UserController發(fā)url請求時(shí)的url也是正確的,我的是:
String url = "http://userservice/user/" + order.getUserId();
其中userservice
一定是你自己在user的.yml配置文件中指定的名稱。
但還是報(bào)錯(cuò),那么你可以在整個(gè)工程的pom文件中添加下面這個(gè)依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
總結(jié)
這個(gè)問題困擾了我兩天,網(wǎng)上各種答案都看遍了,但是不適合我。最終找到了這個(gè)問題的解決方法。
參考文章
文章里面解釋道:
出現(xiàn)這種問題,肯定是springboot視圖解析器出現(xiàn)了問題,那么我們都知道springboot的官方推薦視圖解析器是thymeleaf,所以如果程序要跳轉(zhuǎn)到頁面,應(yīng)該加入thymeleaf依賴。
之后,再啟動(dòng)整個(gè)項(xiàng)目,訪問http://localhost:8080/order/101
時(shí),就會(huì)出現(xiàn)下面這個(gè)正確結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-525191.html
{
"id": 101,
"price": 699900,
"name": "Apple 蘋果 iPhone 12 ",
"num": 1,
"userId": 1,
"user": {
"id": 1,
"username": "柳巖",
"address": "湖南省衡陽市"
}
}
并且實(shí)現(xiàn)了負(fù)載均衡。文章來源地址http://www.zghlxwxcb.cn/news/detail-525191.html
到了這里,關(guān)于黑馬微服務(wù)課程cloud-demo項(xiàng)目出現(xiàn)No instances available for userservice錯(cuò)誤解決辦法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!