gateway的lb路由失效
情况一:http://可以正常路由但是lb://服务名的形式不行
原因:对比了下没问题的练习项目,低版本的gateway依赖中有ribbon和loadblancer相关依赖。
可能造成这种情况的其他原因:
目标服务没有正确的注册到nacos中;
目标服务注册的namespace或者group不同(这是网上看来的,没试过)。
解决办法:加一个依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
情况二:一开始503,后来变成了500。
原因:中途做了一个操作就是去改了被调用服务的服务名,从nacos-service改成了nacos_service。
解决办法:改回 - 就行了
nacos配置值获取错误
情况:代码是使用阿里镜像生成的springboot项目创建时,选择引入依赖后自带的示例代码。代码中获取远程动态配置的类为User类,属性为name和age。其中age可以正常获取,但是name却总是获取到我操作系统的用户名称(不知道是不是操作系统的用户名,只是相同,不确定来源),获取name用的是user.name,nacos远程配置文件也是如此设置。
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zm.nacos.nacosconfig;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "user")
public class User implements InitializingBean, DisposableBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("[afterPropertiesSet()] " + toString());
}
@Override
public void destroy() throws Exception {
System.out.println("[destroy()] " + toString());
}
}
nacos:
nacos:
name: qweqe
age: 30
验证猜想:
@ConfigurationProperties(prefix = "user")的user改为其他任意字符,比如给我整 “le” 了,更改后可以正确获取。
再有就是保持注解不变,修改name属性名为userName或其他,nacos远程配置文件也一样做修改,更改后可正确获取。
原因不详,数据来源不详。有大佬知道求指教!!!