17.SpringCloud combat project-SpringCloud integrates Alibaba-Nacos components

SpringCloud combat project full set of learning tutorials in serial

PassJava learning tutorial

Introduction

  • The PassJava-Learning project is a learning tutorial for the PassJava project. Explain the main points of architecture, business and technology.
  • PassJava is an 面试刷题open source system for Java . You can use small programs to view common interview questions in a fraction of the time to consolidate the foundation of Java.
  • PassJava project can teach you how to build SpringBoot project, Spring Cloud project
  • Use popular technologies such as SpringBoot, MyBatis, Redis, MySql, MongoDB, RabbitMQ, Elasticsearch, and adopt Docker containerized deployment.

Better reading experience

Document serialization directory

Integrate Spring Cloud Alibaba Nacos components

Nacos is Alibaba's open source dynamic service discovery, configuration management and service management platform that is easier to build cloud-native applications.

1. Introduce Nacos service discovery component

The pom.xml file of the passjava-common module introduces the Nacos service discovery component

<!-- nacos discovery 服务发现组件-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2. Download and start Nacos Server

  • Download the Nacos Server compressed package

https://github.com/alibaba/nacos/releases

Start the Server, enter the decompressed folder or the compiled and packaged folder, find the following relative folder nacos / bin, and compare the following commands under the actual operating system.

  1. Linux / Unix / Mac operating system, execute commands sh startup.sh -m standalone
  2. Windows operating system, execute commands cmd startup.cmd

Windows encountered problems executing startupm.cmd:

λ startup.cmd                                                      
 Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! 

solution:

Modify% JAVA_HOME% in startup.cmd file

%JAVA_HOME% 替换为 C:\Program Files\Java\jdk1.8.0_131

Successful start:

nacos server start result

3. Each microservice is configured with Nacos Server address

  • Configure Nacos Server address

Configure the Nacos Server address in the /src/main/resources/application.yml configuration file of the passjava-question, passjava-channel, passjava-content, passjava-member, and passjava-study applications

spring:
   cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

4. Add annotations

Use @EnableDiscoveryClient annotation for each service to enable service registration and discovery

@EnableDiscoveryClient
@MapperScan("com.jackson0714.passjava.question.dao")
@SpringBootApplication
public class PassjavaQuestionApplication {

    public static void main(String[] args) {
        SpringApplication.run(PassjavaQuestionApplication.class, args);
    }

}

5. Configure the name of the microservice

spring:
  application:
    name: passjava-question

6. Access the nacos server background

  • Login background

http://localhost:8848/nacos/index.html#/login

Username: nacos

Password: nacos

nacos server background
  • View registered services

    passjava-channel 渠道微服务
    passjava-member 用户微服务
    passjava-study 学习微服务
    passjava-question 问题微服务
    passjava-content 内容微服务
    
    Registered service

Code address

https://github.com/Jackson0714/PassJava-Platform

No public

No public

Guess you like

Origin www.cnblogs.com/jackson0714/p/12730812.html