Chapter III Alibaba cloud core component service governance Nacos

1. What is the registry of micro services

  • Microservice architecture diagram

  • What is a registry (service governance)
    Service registration: the service provider reports its own network information to the registration center when it is started
    Service discovery: the service consumer reports its own network information to the registration center when starting, and pulls the relevant network information of the provider
    Core: service management, which has a service registry and dynamic maintenance of heartbeat mechanism. The service instance is registered to the service registry at startup and logged off at shutdown

  • Why
    There are more and more microservice applications and machines. The caller needs to know the network address of the interface. If the network address is controlled by means of configuration file, it will bring great problems to the maintenance of dynamically added machines

  • Mainstream registries: zookeeper, Eureka, consumer, etcd, Nacos
    The best combination of Alibaba cloud is Nacos, and in addition to the registration and discovery of services, it also supports dynamic configuration services

2. Alibaba cloud registration center Nacos actual combat

1) Introduction to registration center Nacos
Official website:

2) Install JDK
Official website:
Unzip the downloaded compressed package to / home/edric/Tools
Modify the etc/profile file and append at the end

export JAVA_HOME=/home/edric/Tools/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

Execute source /etc/profile
Execute java version view

3) Install Maven
Official website:
After downloading, unzip the file to / home/edric/Tools
Modify the etc/profile file and append at the end

export PATH=$PATH:/home/edric/Tools/maven/bin

Execute source /etc/profile
Execute mvn -version
4) Install nacos
git address:

Unzip the installation package
Enter the bin directory
Start sh startup sh -m standalone
Visit localhost:8848/nacos
Default account password: nacos/nacos
be careful:
The browser cannot access the git website. Check whether the browser has set up a proxy and change it to not use a proxy
The local host cannot access the nacos in the virtual machine. The solution is as follows: close the centos firewall

systemctl stop firewalld service
#Boot disable
systemctl disable firewalld

3. Call between order and video services based on Nacos

1) Video service integration Nacos
Add dependency

        <!--add to nacos client-->

Configure Nacos address

  port: 8000

    name: xdclass-video-service

Add annotation to startup class

public class VideoApplication {
    public static void main(String[] args) {,args);

2) Order service integration Nacos
3) User service integration Nacos
4) Start service
5) Calls between services

public class OrderController {
    private RestTemplate restTemplate;
    private DiscoveryClient discoveryClient;
    public Object save(int videoId){
        //Video video = restTemplate.getForObject("http://localhost:9000/api/v1/video/find_by_id?videoId="+videoId, Video.class);
        List<ServiceInstance> list =  discoveryClient.getInstances("xdclass-video-service");
        ServiceInstance serviceInstance = list.get(0);
        Video video = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/api/v1/video/find_by_id?videoId="+videoId, Video.class);
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setCreateTime(new Date());
        return videoOrder;

public class OrderApplication {

    public static void main(String [] args){,args);
    public RestTemplate getRestTemplate(){
        return new RestTemplate();

Keywords: Java Spring Cloud eureka

Added by tomz0r on Mon, 31 Jan 2022 07:15:12 +0200