background
In 2019, the first blog intends to start from the micro service, is the so-called their own flag lie lie also want to finish writing ^^. So I plan to continue writing spring-Cloud articles from today.
What are microservices
- Microservices are an architectural style
- A series of tiny services
- Each service is developed and deployed independently
Why microservices are used
Because there are some problems in the use of monomer, it is summarized as follows:
- Low development efficiency (large code system)
- Difficult code maintenance (large code system)
- Poor stability (changing one module may affect other code functions)
- Expansion difficulty (single module extension needs to consider whether it affects other module functions)
- Inflexible deployment (long deployment time and cumbersome deployment)
What is the Eureka
- Eureka: As the name suggests, Eureka’s role in Spring-Cloud is registration and discovery of services
- Spring Cloud Eureka is a secondary encapsulation based on Netflix Eureka
- The Spring Cloud Eureka component consists of two parts: Eureka-Server and Eureka-client
- Eureka-server: service registry
- Eureka-client: service registration and service invocation
instructions
- In the series of blogs, the version of Spring-Cloud is Greenwich.RC1
- IDE USES the idea of
- Spring-boot version :2.1.1.RELEASE. We can go to the official website to check the corresponding spring-Cloud version and Boot version
Set up Eureka – Server
-
Open IDEA and select Create Spring project
-
Select Maven coordinate: GAv
-
Select eureka-Server as the project type
-
After creating the project, check the Spring-Cloud and Boot versions in the Pom:
<! -- Spring-boot version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<! -- Spring-cloud version -->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC1</spring-cloud.version>
</properties>
Copy the code
- As eureka-server we need to add the @enableEurekaserver annotation to the startup class
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code
- Modifying configuration files
- Since bootstrap.yml/properties in Spring-Cloud (yML in this case) is the startup load configuration file for the project, we first rename the configuration file to bootstrap.yml
- As a service registry, we require high reliability and stability, so we set up three sets of Eureka-Servers with ports 8761, 8762 and 8763 respectively. The project construction method is the same (same as above). The configuration file of Eureka-Server1 is as follows:
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
register-with-eureka: false
spring:
application:
name: eureka-server
server:
port: 8761
Copy the code
The configuration files of Eureka-server2 are as follows:
spring:
application:
name: eureka-server2
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
register-with-eureka: false
Copy the code
The configuration files of Eureka-server3 are as follows:
spring:
application:
name: eureka-server3
server:
port: 8763
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
Copy the code
Configuration description:
- Spring.application. name: is the name of the service
- Server. port: indicates the service port
- Register-with-eureka =false Indicates that the eureka-server does not need to register itself
- As can be seen from the configuration of the three Eureka-Servers above, in the registered Eureka-server cluster, we only need to register different Eureka-servers with each other to realize the high availability of Eureka-Server
- After starting the service, we can access :http://localhost:8761/; http://localhost:8762/; http://localhost:8763/ Found that the registry is started:
At the end
Now that our clustered Eureka-Server is set up, let’s set up eureka-Client to discover services in the next section. Well, how to predict the future, please listen to the next decomposition!