With the increase of business requirements, many enterprises face the problem of serious code coupling and low efficiency. Spring launched SpringCloud after Netflix opened source its own microservices architecture. At present, the threshold of SpringCloud is relatively low, you can get started after understanding the general SpringCloud, more configuration, or routine things. Of course, I say so just for students who want to get started quickly, the source code is extensive and profound, and can be more interested in research. Do their own micro services also do half a year, now write a demo, summarize the basic usage of SpringCloud, let our micro services car to start first.

Spring – the cloud – microservice code

Project a

The project is in the form of module. A project is divided into multiple modules, which makes it convenient to import the project.

  • []Eureka Registry
  • []Config Configuration center
  • []Oauth2 certification center
  • [] Zuul gateway
  • [] API – admin service

Registry Eureka

Eureka serves as the registry of services, and calls between services are completed through Eureka. All services register themselves in Eureka. When service A wants to call service B, service A only needs to use Service B’s instanceId instead of IP to complete the call. In distributed applications, where services are randomly deployed across servers, it’s extremely inefficient to call services based on IP, so you write code. When the service starts multiple instances, typically using ribbon and Feign, the load is automatically balanced without intervention.

Starting a Eureka service registry is also simple to configure. Add the spring-cloud-starter-Eureka-server dependency to pop.xml and add the @enableeurekaserver annotation to the entry class.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
Copy the code
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}Copy the code

If Eureka only wants standalone mode (just start an instance), there is no need to configure the content in the configuration file application.yml, just the basic information and port. If you want to start the HighAvailability mode (that is, start multiple instances), you can refer to the configuration.

At deployment time, use the same compiled JAR package and enter different parameters after startup. For example, if you run the java-jar-dspring.profiles. active=master your_jar_name. The content on the top is public configuration. When starting the master configuration, the actual configuration is public configuration + Master configuration. If some configurations have both, the master configuration overrides the common configuration.

# Public configuration server: port: 8080 Spring: application: name: eureka-server eureka: instance: lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 5 prefer-ip-address: true client: register-with-eureka: true fetch-registry: True -- # configure master Spring: profiles: master Eureka: instance: hostname: master Client: service-url: defaultZone: http://master:8080/eureka/, http://backup1:8080/eureka/, http://backup2:8080/eureka/ - # configuration backup1 spring: profiles: backup1 eureka: client: service-url: defaultZone: http://master:8080/eureka/, http://backup1:8080/eureka/, http://backup2:8080/eureka/ - # configuration backup2 spring: profiles: backup2 eureka: client: service-url: defaultZone: http://master:8080/eureka/,http://backup1:8080/eureka/,http://backup2:8080/eureka/Copy the code

Here are a few key configurations

Eureka. Client. registrie-fetch -interval-seconds Specifies the interval at which the service obtains registration information from Eureka. The default interval is 30s.

Eureka. Instance. lease-renewal- interval-seconds Specifies the heartbeat interval between the service and Eureka. The default value is 30 seconds. If the instance implements HealthCheckCallback and decides to make itself unavailable, the instance will not receive traffic either.

Lease -expiration- durations-in-seconds Specifies the time for the eureka to wait for the next heartbeat after receiving the last heartbeat. If the timeout occurs, the eureka instance is deleted. The default value is 90s.

Eureka.server. enable-self-preservation Specifies whether to enable the self-preservation mode. The default value is true. By default, If Eureka Server does not receive a heartbeat from a microservice instance within a certain period of time, the Eureka Server will log out of the instance (90 seconds by default). However, when a partition failure occurs and the microservice cannot communicate with the Eureka Server, this behavior can become very dangerous — the microservice itself is healthy and should not be logged out. Eureka solves this problem by going into “self-protected mode” — when a Eureka Server node loses too many clients in a short period of time (possibly due to a network partition failure), the node goes into self-protected mode. Once in this mode, Eureka Server protects the information in the service registry and does not delete the data in the service registry (that is, does not unregister any microservices). When the network is recovered, the Eureka Server automatically exits the self-protection mode. To sum up, self-protection mode is a security protection measure to deal with network anomalies. Its architectural philosophy is to keep all microservices simultaneously (healthy and unhealthy microservices will remain) rather than blindly cancel any healthy microservices. Using self-protection mode can make Eureka cluster more robust and stable.

Eureka.server.eviction- interval-timer-in-MS Specifies the time interval for eureka to clean invalid nodes. The default value is 60,000ms.

Register-with-eureka specifies whether to register Eureka with the client. The value is true in multiple instances.

Eureka.client.fetch -registry indicates whether to pull the registered service. Suppose that service A registers only with the Eureka of the master node, but if this option is turned on, all Eureka nodes register with the service.

Eureka. Client. DefaultZone says it wants to register had been address, format for http://ip:port/eureka/, if there is a DNS deployment environment, can also be IP to domain name, if any is ha mode, configure multiple addresses with commas.

The above are relatively important Eureka configurations.