Microservice introduction and use spring boot to build

Table of contents

1. What is microservice

2. Use spring boot to build microservices

  1. Create a Spring Boot project

  2. Configure RabbitMQ

  3. Create a message producer

  4. Create a message consumer

  5. Run the application and test


1. What is microservice

Microservices (Microservices) is an architectural style of software development that splits an application into a set of small, independent services. Each service runs in its own process and interacts with other microservices using lightweight communication mechanisms. These independent services can be developed, tested and deployed by different teams, making the application easier to maintain, upgrade and expand.

The advantages of microservices architecture include:

  1. Independence: Different services can be developed, tested and deployed independently, making collaboration between teams more flexible and efficient;
  2. Scalability: Different services can be expanded horizontally or vertically according to actual needs, improving the performance and reliability of applications;
  3. Fault tolerance: Since each service is deployed independently, when a service fails, the entire application will not be affected;
  4. Technical diversity: Different services can use different programming languages ​​and technology stacks, allowing teams to choose the most suitable tools and technologies;
  5. Maintainability: Since each service is relatively small, it is easier to maintain and modify individual services.

2. Use spring boot to build microservices

  1. Create a Spring Boot project

First, you need to create a Spring Boot project. You can use Spring Initializr for a quick build: https://start.spring.io/. Select the appropriate options, such as Maven/Gradle, Spring Boot version and dependencies, then download and unzip the generated project.

   2. Configure RabbitMQ

Add the following dependencies in the pom.xml file to use RabbitMQ in the application:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Then, add the following configuration in the application.properties or application.yml file:

spring.rabbitmq.host=localhost # RabbitMQ 服务器地址
spring.rabbitmq.port=5672 # RabbitMQ 端口号
spring.rabbitmq.username=admin # RabbitMQ 用户名
spring.rabbitmq.password=secret # RabbitMQ 密码

 

  3. Create a message producer

Creating a message producer is very simple using the Spring AMQP library. Just define a method to send messages and annotate it with @Bean and @ConfigurationProperties. For example:

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class ProducerApplication {

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

    @Bean
    public Queue queue() {
        return new Queue("hello");
    }

    @Component
    @ConfigurationProperties(prefix = "rabbitmq")
    public class Producer {

        @Autowired
        private RabbitTemplate rabbitTemplate;

        private String exchange;
        private String routingkey;

        public void send(String message) {
            rabbitTemplate.convertAndSend(exchange, routingkey, message);
        }

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public String getRoutingkey() {
            return routingkey;
        }

        public void setRoutingkey(String routingkey) {
            this.routingkey = routingkey;
        }
    }
}

In the code above, we first define a queue called "hello". Then we define a class called "Producer" which contains a sendMessage() method that can send a message to the queue.

 

     4. Create a message consumer

Creating a message consumer is also very simple using the Spring AMQP library. Just define a method to receive messages and annotate that method with @RabbitListener. For example:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @RabbitListener(queues = "hello")
    public void receive(String message) {
        System.out.println("Received: " + message);
    }
}

In the above code, we have defined a class called "Consumer" which contains a receive() method for receiving messages from the queue. This method is annotated by @RabbitListener.

 

      5. Run the application and test

Now that you have a producer and consumer in place, you can start the application and test messaging. Run the following command at the command line to start the application:

mvn spring-boot:run

Then, after the producer sends a message, the consumer will receive that message and display it on the console.

Guess you like

Origin blog.csdn.net/weixin_59367964/article/details/130293082