The simplest spring cloud tutorial in history chapter 1: service registration and discovery (Eureka)
1, Introduction to spring cloud
Spring cloud provides developers with some tools to quickly build distributed systems, including configuration management, service discovery, circuit breaker, routing, micro agent, event bus, global lock, decision election, distributed session and so on. It runs in a simple environment and can run on developers' computers. In addition, spring cloud is based on springboot, so you need to have a certain understanding of springboot in development. If you don't, you can see this article: 2 hours to learn springboot . In addition, if you don't know about "micro service architecture", you can search "micro service architecture" through the search engine.
2, Create service registry
Here, the component we need to use is Eureka of Spring Cloud Netflix. Eureka is a service registration and discovery module.
2.1 first create a maven main project.
**2.2 then create two model projects: * * one model project is used as the service registry, that is, Eureka Server, and the other is used as Eureka Client.
The following describes the creation process in detail by taking the creation of a server as an example:
Right click Project - > create model - > select spring initialir, as shown in the following figure:
Next - > select cloud discovery - > Eureka server, and then go to the next step.
The pom.xml file of the created project is as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>eurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eurekaserver</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- spring boot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
2.3 to start a service registry, only one annotation @ EnableEurekaServer is required. This annotation needs to be added to the startup application class of the springboot project:
@EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
**2.4 **eureka is a highly available component. It has no back-end cache. After each instance is registered, it needs to send a heartbeat to the registry (so it can be completed in memory). By default, eureka server is also a eureka client, and a server must be specified. eureka server configuration file application.yml:
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Use eureka.client.registerWithEureka: false and fetchRegistry: false to indicate that you are a eureka server
2.5 eureka server has an interface. Start the project and open the browser to access:
http://localhost:8761 , the interface is as follows:
No application available no service found_
Because there is no registered service, of course, no service can be found.
3, Create a service provider (eureka client)
When the client registers with the server, it will provide some metadata, such as host and port, URL, home page, etc. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is usually deleted from the registered server.
The creation process is similar to that of server. After creating pom.xml, it is as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-hi</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-hi</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
The annotation @ EnableEurekaClient indicates that you are an eurekaclient
@SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Only @ EnableEurekaClient is not enough. You also need to indicate the address of your service registry in the configuration file. The application.yml configuration file is as follows:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: service-hi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
spring.application.name needs to be specified. This is very important. It is generally based on this name when calling between services in the future.
Start project, open http://localhost:8761 , the website of eureka server:
You will find that a service has been registered in the service. The service name is SERVICE-HI and the port is 7862
Now open http://localhost:8762/hi?name=forezp , you will see on the browser:
hi forezp,i am from port:8762
The simplest spring cloud tutorial in history | Chapter 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Chapter 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Chapter 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Chapter 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Part 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Part 1: service registration and discovery (Eureka) the simplest spring cloud tutorial in history | Part 1: service registration and discovery (Eureka)