table of Contents
1 Introduction
nacos
Second generation micro-services SpringCloudAlibaba
revenue distributed to a distribution center and the registry component, its function is a first generation of micro-services SpringCloud
in Eureka
and Config
combination. In short, Nacos can achieve 分布式服务注册与发现
and 分布式配置
central functions.
Nacos official document: https://nacos.io/zh-cn/docs/what-is-nacos.html
2.Nacos-discovery supports Ribbon load balancing by default
In order to keep curiosity, specially analysis of what SpringCloud common registry components: Eureka
, Consul
, nacos
etc., are all integrated and open the default Ribbon
load balancing, this section is nacos theme, we analyze under nacos service discovery component, easy to see, he It is also integrated by default and supports ribbon load balancing.
3. Environment setup
3.1.Product provides interface
package com.bruce.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @BelongsProject: springcloud-alibaba-nacos
* @BelongsPackage: com.bruce.controller
* @CreateTime: 2021-02-18 13:37
* @Description: 生产者——>商品服务
*/
@Slf4j
@RestController
public class ProductController {
@Value("${server.port}")
private String port;
@GetMapping("/getProductMsg")
public String getProductMsg() {
log.info("开始调用商品服务信息啦");
return "我是商品服务, 调用商品服务接口成功了===>> : " + port;
}
}
3.2.Order the interface of consumer goods (3 ways)
package com.bruce.controller;
/**
* @BelongsProject: springcloud-alibaba-nacos
* @BelongsPackage: com.bruce.controller
* @CreateTime: 2021-02-18 13:47
* @Description: TODO
*/
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @desc: 消费者——>订单服务
* @author: cao_wencao
* @date: 2020-04-17 20:58
*/
@Slf4j
@RestController
public class OrderController {
@Autowired(required = false)
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private DiscoveryClient discoveryClient;
//方式一 :通过IP地址
@GetMapping("/getProductMsg")
public String getProductMsg() {
String url = "http://127.0.0.1:8715/getProductMsg";
String response = restTemplate.getForObject(url, String.class);
log.info("response==>: {}",response);
return response;
}
//方式二 :通过服务名(这种方式不可行,在Eureka中可以)
@GetMapping("/getProductMsg2")
public String getProductMsg2() {
String url = "http://nacos-product/getProductMsg";
String response = restTemplate.getForObject(url, String.class);
log.info("response==>: {}",response);
return response;
}
//方式三 :通过LoadBalancerClient获取服务调用地址进行调用
@GetMapping("/getProductMsg3")
public String getProductMsg3() {
//discoveryClient.getInstances()
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-product");
String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort() + "/getProductMsg");
String response = restTemplate.getForObject(url, String.class);
log.info("response结果==>>>: {}", response);
return response;
}
//方式四 :通过LoadBalancerClient获取服务调用地址进行调用
@GetMapping("/getProductMsg4")
public String getProductMsg4() {
List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("nacos-product");
//任意选择一个,实现本地RPC调用
ServiceInstance serviceInstance = serviceInstanceList.get(0);
String response = restTemplate.getForObject(serviceInstance.getUri()+ "/getProductMsg", String.class);
log.info("response结果==>>>: {}", response);
return response;
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4. Test based on RestTemplate
When using nacos as a registration center, there are two types of service consumption, one is RestTemplate, and the other is Feign client. Among them, RestTemplate has 3 ways to implement service consumption.
Start the project, observe the nacos console first, and find that the services are all registered
- Call by IP address: http://127.0.0.1:8710/getProductMsg
- Get the service call address through LoadBalancerClient to call: http://127.0.0.1:8710/getProductMsg3
- Get the service call address through DiscoveryClient to call: http://127.0.0.1:8710/getProductMsg4