Micro service

Microservice architecture is the evolution of single application. Due to the rapid development of the Internet industry, it is an architecture system that emerges at the right moment. It has the advantages of independent decoupling, high availability, strong scalability and easy deployment. As well as providing a range of benefits, inservice also brings architectural complexity. After the application of microservitization, problems such as service discovery, service configuration management and service authorization verification will be met. The following is a typical microservice architecture diagram. This paper uses Spring Cloud to build a simple microservice architecture.

What is Spring Cloud?

Spring Cloud is a collection of frameworks for building microservices development and governance. Its main modules are as follows:

Eureka: Service registry for service management. Ribbon: Client-based load balancing component. Hystrix: Fault-tolerant framework that protects against service avalanche effects. Feign: Web service client that simplifies calls to HTTP interfaces. Zuul: API gateway that provides functions such as route forwarding and request filtering. Config: distributed configuration management. (EG: Apollo) Sleuth: Service tracking Stream: A framework for building message-driven microservice applications. Bus: The cluster message Bus of the message broker.

Eureka Registry

Eureka is the Spring Cloud registry, providing service registration and service discovery functions.

Quickly create a Eureka application using IDEA

1. The use ofstart.spring.ioCreate the Spring Cloud Eureka application

2. Select Eureka Server

3. Fill in the project name to complete the construction project

4. Add the configuration in the application.properties file

# Project name
spring.application.name=hello-eureka-new  
 # portServer port = 8761 eureka. Client. Service - url. DefaultZone = http://127.0.0.1:8761/eureka/# Register with Eureka
eureka.client.register-with-eureka=false  
# Whether to get synchronization information from eureka
eureka.client.fetch-registry=false  
# Clear invalid node time
eureka.server.eviction-interval-timer-in-ms=10000
Eureka will calculate that if the rate of heartbeat failure is less than 85% within 15 minutes, the protection mechanism will be triggered. Service providers will not be excluded. If the service registry is closed, unavailable instances will be correctly excluded
eureka.server.enable-self-preservation=false
Copy the code

5. Add the @enableeurekaserver annotation to the startup class

7. After the two Web projects are created, introduce the Eureka dependency in POM.xml

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
Copy the code

And configure the following in application.properties:

Service registry configuration, specifying the Url of the service registryEureka. Client. ServiceUrl. DefaultZone eureka. = http://127.0.0.1:8761/eureka/ instance. -- - IP address =true
Copy the code

The bootstrap class adds an annotation @enableeurekaclient

Feign call

Feign is a declarative REST client that makes REST calls easier. Feign provides a template for HTTP requests. By writing a simple interface and inserting annotations, you can define HTTP request parameters, format, address, and so on. Feign, on the other hand, proxies HTTP requests completely, simply calling it like a method to complete the service request and related processing. Spring Cloud encapsulates Feign to support SpringMVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and the Ribbon to support load balancing.

The steps to integrate Feign in Spring Cloud are fairly simple. First, add the Feign dependency annotation @enableFeignClients to the startup class. The dependency information is as follows:

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
Copy the code

Defines a Feign client in the form of an interface annotated with @FeignClient. This annotation identifies that it is currently a Feign client, and the value attribute is the corresponding service name, which can be invoked directly by injecting UserRemoteClient, just as a local method is invoked to the developer. Here, the feign configuration file (Configuration = FeignConfiguration.class) is introduced to configure the log level of remote call printing, which facilitates us to see the RPC call process.

Modify the (hello-july-two) FoodControl implementation to return the (hello-July-one) UserDto message

The postman call results in the following:

conclusion

A simple microservice architecture at this point would have a service registry and business services could call each other, Future articles will include configuration centers for services (Apollo), unified gateways for services (Gateway), load balancing for services, fault tolerance for services (Hystrix), integration middleware (Redis, MQ, ES), etc.

The last

Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!