Amateur Grass Springcloud Tutorial | First article: Registration and discovery of services Eureka (Finchley version)

Source: Internet
Author: User

Introduction of Spring Cloud

Since the simplest spring cloud tutorial in history is very popular with readers, I have specifically upgraded the version, which currently supports the Spring boot version 2.0.3.release,spring cloud version finchley.release.

The official documentation for the Finchley version is as follows:
Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-proxies, event busses, global locks, decision-making campaigns, distributed sessions, and more. It runs in a simple environment and can run on a developer's computer. In addition, Spring Cloud is based on springboot, so need to develop a certain understanding of springboot, if you do not understand the words can read this article: 2 hours to learn springboot. In addition to the "micro-service Architecture" Do not understand, you can search through the search engine "microservices architecture" under the understanding.

Second, create the Service registration center

Here, I still use Eureka as a service registration and discovery components, as for the consul will be published in detail.

2.1 First create a MAVEN master project.

First, create a master Maven project that introduces dependencies in its pom file, and the Spring boot version is 2.0.3.release,spring cloud version finchley.release. This pom file acts as a parent pom file that relies on version control, and other module projects inherit the POM. This series is all used in this model, and the other article Pom is the same as this pom. Once again, it is no longer a repeat introduction. 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"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>sc-f-chapter1</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>pom</packaging>Ten   One<name>sc-f-chapter1</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-parent</artifactId> -<version>2.0.3.RELEASE</version> -<relativePath/> +</parent> -   +<modules> A<module>eureka-server</module> at<module>service-hi</module> -</modules> -   -<properties> -<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> in<java.version>1.8</java.version> -<spring-cloud.version>Finchley.RELEASE</spring-cloud.version> to</properties> +   -<dependencies> the<dependency> *<groupId>org.springframework.boot</groupId> $<artifactId>spring-boot-starter-test</artifactId>Panax Notoginseng<scope>test</scope> -</dependency> the</dependencies> +   A<dependencyManagement> the<dependencies> +<dependency> -<groupId>org.springframework.cloud</groupId> $<artifactId>spring-cloud-dependencies</artifactId> $<version>${spring-cloud.version}</version> -<type>pom</type> -<scope>Import</scope> the</dependency> -</dependencies>Wuyi</dependencyManagement> the   -<build> Wu<plugins> -<plugin> About<groupId>org.springframework.boot</groupId> $<artifactId>spring-boot-maven-plugin</artifactId> -</plugin> -</plugins> -</build> A   +</project>

2.2 Then create 2 model projects: One model project as the service registry, the Eureka Server, and the other as the Eureka Client.

The following is an example of creating a server, detailing the creation process:

Right-click Project-Create model-> Select spring Initialir such as:

Next, choose Cloud Discovery->eureka Server, and then go the next step.

After the creation of the project, its pom.xml inherits the parent Pom file, and introduces the Spring-cloud-starter-netflix-eureka-server dependency, 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"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>eureka-server</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>eureka-server</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter1</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.cloud</groupId> at<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> -</dependency> -</dependencies> -   -</project>

2.3 Start a service registry, you need only one annotation @enableeurekaserver, this annotation needs to be added to the Springboot project's Startup application class:

@SpringBootApplication @enableeurekaserverpublic class Eurekaserverapplication {public     static void Main (string[] args) {        springapplication.run (eurekaserverapplication.class, args);}    }

2.4 Eureka is a highly available component that does not have a back-end cache, each instance is registered to send a heartbeat to the registry (so it can be done in memory), and by default Erureka server is also a Eureka client, and you must specify a server. Eureka Server configuration file Appication.yml:

1 Server:2port:87613  4 Eureka:5 instance:6 Hostname:localhost7 Client:8Registerwitheureka:false9Fetchregistry:falseTen serviceurl: OneDefaultzone:http://${eureka.instance.hostname}:${server.port}/eureka/ A   - Spring: - Application: theName:eurka-server

by Eureka.client.registerWithEureka:false and Fetchregistry:false to show that you are a Eureka server.

2.5 Eureka Server is an interface that starts the project and opens the browser to access:
http://localhost:8761, the interface is as follows:

No application available no service was found ... ^_^
Because there is no registration service of course there is no service was found.

Third, create a service provider (Eureka client)

When the client registers with the server, it provides some metadata, such as the host and port, the URL, the home page, and so on. Eureka server receives heartbeat messages from each client instance. If the heartbeat times out, the instance is typically removed from the registration server.

The creation process is similar to the server, and the Pom.xml is created 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"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>service-hi</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>service-hi</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter1</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.cloud</groupId> at<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> in</dependencies> -   to<build> +<plugins> -<plugin> the<groupId>org.springframework.boot</groupId> *<artifactId>spring-boot-maven-plugin</artifactId> $</plugin>Panax Notoginseng</plugins> -</build> the   +<!--www.xttblog.com-- A</project>

The annotated @enableeurekaclient shows that he is a eurekaclient.

@SpringBootApplication @enableeurekaclient@restcontrollerpublic class Servicehiapplication {public     static void Main (string[] args) {        springapplication.run (servicehiapplication.class, args);    }     @Value ("${server.port}")    String port;     @RequestMapping ("/hi") public    string Home (@RequestParam (value = "Name", defaultvalue = "Forezp") string name) {        Return "HI" + name + ", I am from port:" + Port;    }}

Just @enableeurekaclient is not enough, you also need to specify the address of your service registry in the configuration file, the APPLICATION.YML configuration file is as follows:

1 Server: 2   port:8762 3   4Spring:  5  application:  6     Name: service-Hi  7   8Eureka:  9  client:  Ten    serviceurl:       defaultzone:http://localhost:8761/ eureka/  

It is important to specify the Spring.application.name, which is typically based on this name in the subsequent invocation of services and services.
To start the project, open http://localhost:8761, which is the Eureka server URL:

You will find a service already registered in the service, the service name is Service-hi, the port is 7862

When you open Http://localhost:8762/hi?name=forezp, you will see it on your browser:

Hi forezp,i am from port:8762

SOURCE Download: Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter1

Iv. references

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

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | First article: Registration and discovery of services Eureka (Finchley version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.