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: https://nacos.io/zh-cn/

2) Install JDK
Official website: https://www.oracle.com/java/technologies/downloads/
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: https://maven.apache.org/download.cgi
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: https://github.com/alibaba/nacos/releases


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

#close
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-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

Configure Nacos address

server:
  port: 8000

spring:
  application:
    name: xdclass-video-service
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.3.104:8848

Add annotation to startup class

@SpringBootApplication
@MapperScan("net.xdclass.dao")
@EnableDiscoveryClient
public class VideoApplication {
    public static void main(String[] args) {
        SpringApplication.run(VideoApplication.class,args);
    }
}

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

//OrderController 
@RestController
@RequestMapping("api/v1/video_order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @RequestMapping("/save")
    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.setVideoId(video.getId());
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setCreateTime(new Date());
        return videoOrder;
    }
}

//OrderApplication 
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {

    public static void main(String [] args){

        SpringApplication.run(OrderApplication.class,args);
    }
    
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    
}

Keywords: Java Spring Cloud eureka

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