Hello,
I'm trying to use cmdb feature to implement calling service closely when there are many provider service instances.
I deployed one consumer service and one provider service on my local machine. And I deployed another provider service on remote server. I've putted cmdb plugin in the directory of {nacos.home}/plugins/cmdb and restart the service. I edited the registered provider service (Screenshot below)
However, when I used consumer service to call provider service, I expeted only local provider service can be called. But remote provider service was called as well. So, selector of label did not work.
Environment and variables:
Nacos server: 2.1.1
Consumer: IP: 10.114.200.154 cluster:cluster1
Local Provider: IP: 10.114.200.154 cluster:cluster1
Remote Provider: IP: 10.114.10.218, cluster:cluster2
Here are my source code below:
Provider:
Project structure
@SpringBootApplication
@EnableNacosDiscovery
public class ProviderApplication {
@NacosInjected
private NamingService namingService;
@Value("${spring.application.name}")
private String springApplicationName;
@Value("${nacos.cmdb.ip}")
private String nacosCmdbIp;
@Value("${nacos.cmdb.port}")
private int nacosCmdbPort;
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@PostConstruct
public void registerInstance() throws NacosException {
namingService.registerInstance(springApplicationName, nacosCmdbIp, nacosCmdbPort);
}
}
@RestController
public class ProviderController {
@Value("${nacos.cmdb.ip}")
private String nacosCmdbIp;
@Value("${nacos.cmdb.cluster}")
private String nacosCmdbCluster;
@GetMapping("/info")
public String info(String fromCluster, String fromIp) {
return String.format("Calling from ip %s, cluster %s. Provided from ip %s, cluster %s",
fromIp, fromCluster, nacosCmdbIp, nacosCmdbCluster);
}
}
Consumer:
Project structure
@SpringBootApplication
@EnableNacosDiscovery
public class ConsumerApplication {
@NacosInjected
private NamingService namingService;
@Value("${spring.application.name}")
private String springApplicationName;
@Value("${nacos.cmdb.ip}")
private String nacosCmdbIp;
@Value("${nacos.cmdb.port}")
private int nacosCmdbPort;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@PostConstruct
public void registerInstance() throws NacosException {
namingService.registerInstance(springApplicationName, nacosCmdbIp, nacosCmdbPort);
}
}
@RestController
public class ConsumerController {
public static final String ERROR = "Error";
@Value("${service.todo-service}") // provider service name
private String todoService;
@Value("${nacos.cmdb.ip}")
private String nacosCmdbIp;
@Value("${nacos.cmdb.cluster}")
private String nacosCmdbCluster;
@Resource
private RestTemplate restTemplate;
@NacosInjected
private NamingService namingService;
Hello, I'm trying to use cmdb feature to implement calling service closely when there are many provider service instances. I deployed one consumer service and one provider service on my local machine. And I deployed another provider service on remote server. I've putted cmdb plugin in the directory of {nacos.home}/plugins/cmdb and restart the service. I edited the registered provider service (Screenshot below) However, when I used consumer service to call provider service, I expeted only local provider service can be called. But remote provider service was called as well. So, selector of label did not work.
Environment and variables: Nacos server: 2.1.1 Consumer: IP: 10.114.200.154 cluster:cluster1 Local Provider: IP: 10.114.200.154 cluster:cluster1 Remote Provider: IP: 10.114.10.218, cluster:cluster2
Here are my source code below: Provider: Project structure
@SpringBootApplication @EnableNacosDiscovery public class ProviderApplication { @NacosInjected private NamingService namingService; @Value("${spring.application.name}") private String springApplicationName; @Value("${nacos.cmdb.ip}") private String nacosCmdbIp; @Value("${nacos.cmdb.port}") private int nacosCmdbPort;
}
@RestController public class ProviderController { @Value("${nacos.cmdb.ip}") private String nacosCmdbIp; @Value("${nacos.cmdb.cluster}") private String nacosCmdbCluster;
}
Consumer: Project structure
@SpringBootApplication @EnableNacosDiscovery public class ConsumerApplication { @NacosInjected private NamingService namingService; @Value("${spring.application.name}") private String springApplicationName; @Value("${nacos.cmdb.ip}") private String nacosCmdbIp; @Value("${nacos.cmdb.port}") private int nacosCmdbPort;
}
@RestController public class ConsumerController { public static final String ERROR = "Error"; @Value("${service.todo-service}") // provider service name private String todoService; @Value("${nacos.cmdb.ip}") private String nacosCmdbIp; @Value("${nacos.cmdb.cluster}") private String nacosCmdbCluster; @Resource private RestTemplate restTemplate; @NacosInjected private NamingService namingService;
}
CMDB plugin public class MyCmdbServiceImpl implements CmdbService { public static final List<Map<String,String>> CMDB_DATA =new ArrayList<>();
}
I've tried calling consumer api and made a summery below. Total:10 Cluster1 Cluster2 Error 5 5 0 50.00 50.00 0.00
I wrote the code with demo on the Internet. Would anyone please point out which part I've done wrong?