Service governance is only responsible for the most core basic modules in the microservice architecture, and it is mainly used to realize the automatic registration and discovery of each microservice instance.
Build a service registry
1. First create a basic Spring boot project, name it eureka-server, and introduce the necessary dependencies in pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <!-- Introduce spring boot dependencies --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Increase the dependency of eureka-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <!-- Introduce spring cloud dependency--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. The @EnableEurekaServer annotation starts a microservice registry
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3. In the default configuration, the service registry will also register itself as a client, so we need to disable its client registration behavior
server.port=1111 #Whether to register yourself to the eureka server eureka.client.registerWithEureka=false #Whether to get registration information from eureka server eureka.client.fetchRegistry=false #Set the interaction address with enreka server, query service and register service multiple addresses, split eureka.client.serviceUrl.defaultZone=http://192.168.30.1:8761/eureka
service providers
1. First create a basic Spring boot project, name it eureka-provider, and introduce the necessary dependencies in pom.xml
<!-- Introduce spring boot dependencies --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <!-- Introduce spring cloud dependency--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement>
2. Create Controller
@GetMapping("/{id}") public User findByid(@PathVariable Long id){ User user = userRepository.findOne(id); return user; }
3. @EnableDiscoveryClient activates @EnableDiscoveryClient implementation
@EnableDiscoveryClient @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
4. Destined to configure the service name and specify the service center address
server.port=9999 server.ssl.enabled=false spring.application.name=microservice-provider-user #This
can be configured with multiple, comma separated eureka.client.serviceUrl.defaultZone=http://192.168.137.100:1111/eureka/
High availability eureka
Modify the eureka project we created before, and create two new configurations application-peer1.properties, application-peer1.properties
server.port=1111 spring.application.name=eureka-server eureka.instance.hostname=peer1 eureka.client.serviceUrl.defaultZone=http://192.168.137.101:1112/eureka eureka.server.enable-self-preservation=false
server.port=1112 spring.application.name=eureka-server eureka.instance.hostname=peer2 eureka.client.serviceUrl.defaultZone=http://192.168.137.100:1111/eureka eureka.server.enable-self-preservation=false
Specify a configuration file to start at startup
nohup java -jar discovery-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 &
nohup java -jar discovery-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 &
In this way, the two eureka can register with each other to form a two-node cluster
eureka self protection mode
When eureka Server does not receive a heartbeat from a microservice instance for a period of time, Eureka Server will log out the instance (default 90 seconds)
When the network partition fails and too many clients are lost, the node will enter the self-protection mode. Once in the protection mode, the Eureka Server will protect the information of the service registry and will not log out.
Disable self-preservation mode with eureka.server.enable-self-preservation=false