SpringCloud tutorial: eureka registry (Finchley Edition)

A, spring cloud Introduction

This phase of the tutorial Spring Boot version 2.0.3.RELEASE, Spring Cloud version Finchley.RELEASE.

Finchley official version of the document as follows: http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud provides a number of tools to quickly build distributed systems for developers, including configuration management, service discovery, circuit breakers, routing, micro broker, event bus, global lock, decision-making campaign, distributed session and so on. It is simple operating environment, it can run on a computer developers. Otherwise spring cloud-based springboot, so there is need to develop some understanding of springboot, if not understand, you can see this article: 2 hours to learn springboot . In addition to "micro Services Architecture" do not understand, you can search for "micro Services Architecture" by search engines understand the next.

Second, create a service registry

Here, I'm still using Component Eureka as a service registration and discovery, as to be out after Consul article details.

2.1 First create a maven main project.

First, create a master Maven project, dependent on the introduction of its pom file, spring Boot version 2.0.3.RELEASE, Spring Cloud version Finchley.RELEASE. The parent pom pom file as a file, play the role of dependent version control, other module works inherit the pom. This series of articles all adopt this model. code show as below:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.liumy</groupId>
 8     <artifactId>eureka-f-1</artifactId>
 9     <version>0.0.1-SNAPSHOT</version>
10     <packaging>pom</packaging>
11 
12     <name>eureka-f-1</name>
13     <description>Demo project for Spring Boot</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>2.0.3.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <modules>
23         <module>eureka-server</module>
24         <module>eureka-client</module>
25     </modules>
26 
27     <properties>
28         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30         <java.version>1.8</java.version>
31         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
32     </properties>
33 
34     <dependencies>
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40 
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-autoconfigure</artifactId>
44             <version>2.0.3.RELEASE</version>
45             <scope>compile</scope>
46         </dependency>
47     </dependencies>
48 
49     <dependencyManagement>
50         <dependencies>
51             <dependency>
52                 <groupId>org.springframework.cloud</groupId>
53                 <artifactId>spring-cloud-dependencies</artifactId>
54                 <version>${spring-cloud.version}</version>
55                 <type>pom</type>
56                 <scope>import</scope>
57             </dependency>
58         </dependencies>
59     </dependencyManagement>
60 
61     <build>
62         <plugins>
63             <plugin>
64                 <groupId>org.springframework.boot</groupId>
65                 <artifactId>spring-boot-maven-plugin</artifactId>
66             </plugin>
67         </plugins>
68     </build>
69 
70 </project>

2.2 model and then create two projects: a model project as a service registry, namely Eureka Server, and the other as Eureka Client.

Below to create a server as an example, the process of creating a detailed description:

Right Project -> Create model-> select spring initialir below:

 

 

 The next step -> select cloud discovery-> eureka server, and then been next on the list.

 

 

 After the project is created, it inherits the parent pom file pom.xml, and the introduction of dependent spring-cloud-starter-netflix-eureka-server, the code is as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.liumy</groupId>
 7         <artifactId>eureka-f-1</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10 
11     <groupId>com.liumy</groupId>
12     <artifactId>eureka-server</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <packaging>jar</packaging>
15 
16     <name>eureka-server</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.cloud</groupId>
22             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
23         </dependency>
24     </dependencies>
25 
26 </project>

2.3 Starting a service registry, only one comment @EnableEurekaServer, need to add this comment on the boot application class springboot project:

 1 package com.liumy.eurekaserver;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class EurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaServerApplication.class, args);
13     }
14 
15 }

Configuration 2.4 yml profile 

eureka component is a highly available, it is not backside cache needs to send a heartbeat to the registration center after each instance is registered (and therefore can be done in memory), erureka server is a eureka Client default, must specify a server . eureka server configuration file appication.yml:

 1 server:
 2   port: 8881
 3 
 4 eureka:
 5   instance:
 6     hostname: localhost
 7   client:
 8     registerWithEureka: false
 9     fetchRegistry: false
10     serviceUrl:
11       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
12 
13 spring:
14   application:
15     name: eurka-server

By eureka.client.registerWithEureka: false and fetchRegistry: false to indicate that he is a eureka server.

2.5 eureka server is the interface to start the project, open the browser to access: http: // localhost: 8761, the interface is as follows:

 

 

 

 

 

 PS: No no services were found to the Application the Available ...... ^ _ ^ because there is no registration service of course there can be no services were found.

Third, create a service provider (eureka client)

When a client registers with the server, it will provide some metadata, such as the host and port, URL, home and so on. Eureka server receiving heartbeat messages from each client instance. If the heartbeat times out, it is usually the instance is deleted from the registration server.

Create a similar process with the server, create the pom.xml as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.liumy</groupId>
 7         <artifactId>eureka-f-1</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10 
11     <groupId>com.liumy</groupId>
12     <artifactId>eureka-client</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <packaging>jar</packaging>
15 
16     <name>eureka-client</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.cloud</groupId>
22             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
23         </dependency>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28     </dependencies>
29 
30     <build>
31         <plugins>
32             <plugin>
33                 <groupId>org.springframework.boot</groupId>
34                 <artifactId>spring-boot-maven-plugin</artifactId>
35             </plugin>
36         </plugins>
37     </build>
38 
39 </project>

It is a eurekaclient by annotation @EnableEurekaClient show.

 1 package com.liumy.eurekaclient;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 import org.springframework.web.bind.annotation.RequestParam;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @EnableEurekaClient
11 @SpringBootApplication
12 @RestController
13 public class EurekaClientApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(EurekaClientApplication.class, args);
17     }
18 
19     @Value("${server.port}")
20     String port;
21     public String Hello(@RequestParam(value = "name",defaultValue = "liumingyu")String name){
22         return "Hello ~"+name+",My Port is:"+port;
23     }
24 }

@EnableEurekaClient just is not enough, also need to address their own service centers registered in the configuration file, application.yml configuration file as follows:

 1 server:
 2   port: 8882
 3 
 4 spring:
 5   application:
 6     name: eureka-client
 7 
 8 
 9 eureka:
10   client:
11     serviceUrl:
12       defaultZone: http://localhost:8881/eureka/

Need to specify spring.application.name, this is very important, which usually call each other based on this name in the future between service and service. Start project, open http: // localhost: 8881, namely eureka server URL:

 

 You will find that a service has been registered in the service, so it is successfully registered, the service name EUREKA-CLIENT, port 8882

Then open http: // localhost:? 8882 / hello name = lmy, you will see in the browser:

Fourth, references

http://blog.csdn.net/forezp/article/details/69696915

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

 

Note: This is the original article, which is the process of teaching and implementation of BAT reference to a top architect to complete.

Guess you like

Origin www.cnblogs.com/lmyupupblogs/p/11706367.html