java.lang.IllegalStateException: Request URI does not contain a valid hostname: /mcroservice-payment/payment/get/35
不管是否是新建的springCloud项目,还是其他情况。
遇到这个问题,相信对springCloud及项目的目录结构有一定的了解,基于springboot。
1.配置的服务名称要规范,不能有下划线
#spring相关配置
spring:
application:
name: hao-payment #服务名
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver #数据库驱动包-org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/hao?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: root
password: 123
2.restTemplate配置类中方法要加注解 @LoadBalanced
package com.hao.utils;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
//RestTemplate 的负载均衡能力
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
3.这也属于忽视的问题
就是在消费者服务的controller中配置的路径要加上http://。
public static final String PAYMENT_URL = "http://mcroservice-payment";
当然可以在其他地方配置。提供一个排除错误的方式。