This paper mainly describes the composition of microservices architecture, what is the role of each component, focusing on the implementation of Eureka Server and Eureka Client practice, in general, service registration and discovery is the basis of microservices entry. Understand and master the service registration and discovery component principle and use method in SpringCloud system, because the access speed of start.spring. IO is really can not be put up with, during the day I compiled the whole SpringBoot initializer, I directly gave the address, so that other friends can also use:
http://139.159.234.67:8080
http://zouchanglin.cn:8080
That’s the IP address up there
If it is still slow, you can download the file I compiled (download address), direct
java -jar Spring-Initializr.jar
Copy the code
In the IDEA custom initializer address fill in http://localhost:8080, note that this version does not have a Web UI, only for the use of the three-point initializer JSON string, the Web UI is almost not used, so I did not consider compiling it!
Simple microservices architecture
The basic framework/components of the microservices architecture
Service registration and discovery
The service provider must register and display its address information before the service caller can discover the target service from the component
Service GateWay Service GateWay
Can access to the service not only to the internal, and there must be a part of the service needs to be exposed to the outside world, so it is easy to understand, the service gateway service gateway is connected inside and outside the door, the gateway will block the background service details, such as the background program to upgrade ah foreign users is no perception, and the function of the routing, Reverse routing can be external request to within a specific service, you can also do some current limit and the function of fault tolerance, because all requests through the gateway, so can control the flow, monitoring and log can also be done in this, such as user authentication, authorization, the crawler is done in the service gateway, effect is very large
Back-end generic services
Also known as the Middle Tier Service, registers itself in the Service registry
The front-end service
Also known as the Edge service Edge Servcice, it invokes desired back-end services from service registration to discovery components
Micro-service is a kind of architecture, the specific implementation is mainly divided into Ali system and SpringCloud system. Ali system mainly uses Dubbo to do service governance, and ZK to do service registry. After Dubbo came back to life in 17 years, it was updated crazily. SpringCloud is a development tool set, including several sub-projects. It takes advantage of the development convenience of SpringBoot to further encapsulate the open source components of Netflix. It basically covers most of the functional components required by micro services. Spring Cloud simplifies the development of distributed applications, and requires a deeper understanding of the characteristics of distributed architecture.
Service registration and discovery
SpringCloud Eureka is a secondary encapsulation based on Netflix Eureka, which mainly consists of two components
- Eureka Server registry
- Eureka Client service registration
The server is the server for service registration. The client is used to simplify the interaction with the server, acts as a polling load balancer, and provides the automatic switching function of the service. The client connects to the service registry and maintains the heartbeat connection, so Eureka can monitor whether the service in the system works properly
Eureka Server
The word Eureka means to have found, to have
First of all, it is necessary to match the version of SpringBoot and SpringCloud, otherwise it may cause many unexpected errors, such as eureka registration result cannot find the service class, such as some Jar import does not enter, etc. The following is the corresponding relationship. The SpringCloud version is named after the London Underground station:
I chose Finchley’s version here
Next, you need to start Eureka Server in the SpringBoot project
Just comment @enableEurekaserver on the startup class. Meanwhile, a Eureka is aServer and a Client, so you need to register yourself. You need to add the following configuration to the configuration file (add spring Application Name so that the name of the registered instance is not UNKOWN) :
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
spring:
application:
name: eureka
Copy the code
To see this, visit localhost:8080 after startup
But as a registry, I don’t want it to be a registered instance on the page, so add the following configuration
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
register-with-eureka: false
Copy the code
Since Eureka’s default port is 8761, this is also changed
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
spring:
application:
name: eureka
server:
port: 8761
Copy the code
Eureka Client
Eureka Discovery Client (spring-boot-starter-web) is the latest version of Eureka Discovery Client.
Next configure @enableDiscoveryClient in the startup class, indicating that this is a Eureka Client, and the configuration file is as follows
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client-01
Copy the code
Open configuration before we had Server, because had the address of the Server is http://localhost:8761/eureka/ so need to configure the address in the configuration, because you like the Eureka Server to register, Then I named myself client-01
Next, I started Eureka Server. In order to facilitate the startup, I directly made a Jar package and made a script on the desktop!
Then start client-01, then open localhost:8761 to see the Eureka client
Note this problem may occur if the Client restarts frequently
The Eureka Server and Eureka Client are connected by heartbeat detection. The Server does not stop to check whether the Client is alive and counts the online rate of the Client at a certain time. The online rate is a proportion. Which means the Client’s online rate is too low, it may be a point in time don’t know you are online or offline, then I’m as you are online, they say, that would rather believe it is, not believe its not, in fact it is a protective mode, in the development environment can turn it off, avoid showing online but in fact is the online, So it’s best to turn this off in a development environment! Now go to the Eureka Server and add the configuration:
eureka: client: service-url: defaultZone: http://localhost:8761/eureka register-with-eureka: false server: Spring: Application: name: Eureka server: port: 8761Copy the code
Keep in mind that the build environment should not be configured this way. After turning this mechanism off, the warning will appear again, which tells us that it is not recommended to turn this mechanism off, but the development environment should be configured this way.