Spring Cloud 集成 Nacos Config 时报 Fetching config from server at : http://localhost:8888

2 篇文章 0 订阅
2 篇文章 0 订阅

事件:

公司Spring Cloud项目原先用的注册中心是Eureka,现在准备换成Nacos,Nacos自身带Config Server,所以打算把Spring Cloud Config 也替换掉。

结果启动时警告信息如下:

2019-08-05 17:46:53.018  INFO 32425 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='user-base-service-dev.yml'}, NacosPropertySource {name='user-base-service.yml'}]}
2019-08-05 17:46:53.027  INFO 32425 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2019-08-05 17:46:53.086  INFO 32425 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2019-08-05 17:46:53.086  WARN 32425 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/user-base-service/dev": 拒绝连接 (Connection refused); nested exception is java.net.ConnectException: 拒绝连接 (Connection refused)
2019-08-05 17:46:53.089  INFO 32425 --- [           main] c.d.c.s.u.b.UserBaseServiceApplication   : The following profiles are active: dev

原因:

看到输出警告的类为:ConfigServicePropertySourceLocator,盲猜可能是Spring Cloud微服务项目在注解了@SpringCloudApplication以后,认为你用的是Spring Cloud Config自动去加载Spring Cloud Config默认的配置信息。但是你并没有配置spring.cloud.config.url属性,所有采用了默认地址:http://localhost:8888

解决:

更改 bootstrap.yml配置文件,添加 spring.cloud.config = false 。

原配置文件为:

spring:
  application:
    name: user-base-service
  profiles:
    active: dev

  cloud:
    nacos:
      config:
        server-addr: 192.168.1.35:8848
        file-extension: yml

更改后为:

spring:
  application:
    name: user-base-service
  profiles:
    active: dev

  cloud:
    config:
      enabled: false
    nacos:
      config:
        server-addr: 192.168.1.35:8848
        file-extension: yml

重新启动微服务,结果成功了。没有了ConfigServicePropertySourceLocator的警告信息。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring和MyBatis是两种流行的Java框架,它们可以相互结合使用以提高应用程序的效率和可维护性。下面是Spring和MyBatis整合的步骤: 1.在pom.xml文件中添加Spring和MyBatis的依赖项: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> ``` 2.创建一个Spring配置文件和MyBatis配置文件,例如applicationContext.xml和mybatis-config.xml。 3.在Spring配置文件中添加以下bean: ```xml <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory" /> </bean> ``` 4.在MyBatis配置文件中配置mapper接口和SQL语句。 5.创建一个mapper接口和对应的SQL映射文件,例如UserMapper和UserMapper.xml。 6.在Spring配置文件中添加以下bean: ```xml <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.example.mapper.UserMapper" /> <property name="sqlSessionTemplate" ref="sqlSession" /> </bean> ``` 7.在Java代码中使用@Autowired注解或者@Resource注解注入mapper接口,例如: ```java @Autowired private UserMapper userMapper; ``` 至此,Spring和MyBatis已经整合完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值