Service registration and Discovery component Eureka Springcloud Micro Service series

Source: Internet
Author: User

I. Introduction to Eurake
Second, the use of Eureka to service the registration of consumption
1. Create a Service registration center
2. Create a service provider
3. Create service Consumers

Summary

I. Introduction to Eurake

Today we introduce the core components of Springcloud Eureka,eurake is responsible for service governance in the MicroServices architecture, responsible for the registration and discovery of each service instance.

The Eureka contains server-side and client components. The server side, also known as the Service registry, is used to provide registration and discovery of services. Client components include service consumers and service producers. When the application is running, the service producer registers its own service instance with the registry, and when the consumer needs to invoke the service, it will first look for the corresponding producer from the registry, and then it can implement the consumption of the service.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
is a relatively simple service registration consumption process, is also a basic operating process of Eureka components. Let's build a Eureka instance.

second, the use of Eureka to service the registration of consumption

1. Create a Service registration center

First of all, open IntelliJ idea, create a springboot project of the main MAVEN project, the name is Eureka, tick the need to use the dependency, to remind that the author here to choose the Springboot version is 2.0.3, it is recommended that readers also choose the same version.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
After you click Next, fill in the project name and click Done.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
In this way, the main project is created successfully, after creating the main project, we need to create two sub-projects, one is the service registry Eureka-server, and the other as the client, we named Eureka-client.

Now, let's create an example of the server, right-click the main project, choose Spring Initialir, New model,

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
Click Next, fill out the information and jump to select the dependent interface, select Eureka Server Dependency

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
The following pom files are created:

<?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.yeya</groupId>

<artifactId>demo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>eureka</name>

<description>demo Project for Spring boot</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.3.RELEASE</version>

<relativePath/> <!--lookup parent from repository to

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>

</project>

To open the service registry, we need to add @enableeurekaserver annotations to the main portal file in the Springboot project

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
By default, Eureka server registers itself with itself, and we need to add some configuration to the configuration file application.yml the following code:

#端口

Server

port:8761

Eureka

Instance

Hostname:localhost

Client

Registerwitheureka:false

Fetchregistry:false

Serviceurl:

defaultzone:http://${eureka.instance.hostname}:${server.port}/eureka/

Set the port for the service to 1111, and turn off the service to register itself by setting Eureka.client.register-with-eureka and Eureka.client.fetch-registry to false.

After doing all this, we right-click the main method to start the project and then access the address in the browser

HTTP://LOCALHOST:1111, the results are as follows:

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
This is the Eureka Registry interface diagram, we can see a lot of information, such as "system Status" can see our environment and the system time and other information, and "General info" has a lot of information about the registration center, including the CPU, free space, etc. These knowledge points are more, and do not affect our project effect, so I do not intend to discuss too much today, see another day to write an article blog for everyone to introduce, we should pay attention to "Instances currently registered with Eureka" This column, The bar shows no instances available, it is obvious that because we do not have a registration service, there is no service available, so we will create the client project Eureka-client again.

2. Create a service provider

The eureka-client is created in a similar way to the server, and is replaced with Eureka Discovery when selecting dependencies, and the following are the created POM files:

<?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.yeya</groupId>

<artifactId>eureka</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>eureka</name>

<description>demo Project for Spring boot</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.3.RELEASE</version>

<relativePath/> <!--lookup parent from repository to

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-eureka-client</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>${spring-cloud.version}</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>

</project>

Again, we need to add annotations to main @enablediscoveryclient

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
Then, add the following configuration to the Eureka-client configuration file:

Set the service name

Spring

Application:

Name:eureka-client

Set up a registry address

Eureka

Client

Service-url:

Defaultzone:http://localhost:1111/eureka

First, specify that your service name is eureka-client, and specify the address of the registry so that the registry can find the service.

Here to illustrate, the idea of the configuration file can be automatically prompted, but to write the registry address can only prompt to Service-url, after the Defaultzone is to be written by the reader, some people will wonder if I wrote wrong, actually not, We can press CTRL to click Defaultzone to jump to its bean class to view the source code.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
As you can see, the default identifier here is really "defaultzone", so the configuration file is not a problem, if you are interested in the configuration of the parsing process, you can follow the source of the code to see.

After the configuration, start the Eureka-client project, and then return to the Eureka-server access interface, you can see our service registration success!

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
In this way, our service registration is complete, then we will test the service consumption process.

3. Create service consumers

Next, we create a service consumer project, named Eureka-consumer, the creation process is the same as Eureka-client, after the successful creation of the main import file to add annotations @enablediscoveryclient, Then modify the configuration file Application.yml:

Set the service name

Spring

Application:

Name:eureka-consumer

Set up a registry address

Eureka

Client

Service-url:

Defaultzone:http://localhost:1111/eureka

Server

port:1113

In order to test the effect of service consumption, we first create an interface DC in the main class in Eureka-client.

@EnableDiscoveryClient

@SpringBootApplication

@RestController

public class Eurekaapplication {

public static void Main (string[] args) {

Springapplication.run (Eurekaapplication.class, args);

}

@GetMapping ("/hello")

Public String DC () {

return "Hello";

}

}

In the Eureka-consumer application main class, initialize the resttemplate to actually initiate the rest request.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
Here, we introduce a little bit about resttemplate, the resttemplate is the network framework used to access the RESTful API, and its main method is related to the rest's HTTP protocol, such as GET, POST, PUT, DELETE, These methods correspond to Getforobject (), getforentity (), Postforobject (), put (), and delete () in this class, and in this article we use Getforobject () to request the API and return the corresponding information.

Create a new controller file and add an interface ("/consumer") to consume the interface provided by the Eureka-client:

@RestController

public class Consumecontroller {

@Autowired

Private Loadbalancerclient client;

@Autowired

Resttemplate resttemplate;

@GetMapping ("/consumer")

Public String consumer () {

Serviceinstance serviceinstance = Client.choose ("eureka-client");

String url = "/http" + serviceinstance.gethost () + ":" + serviceinstance.getport () + "/hello";

String DC = resttemplate.getforobject (Url,string.class);

return DC;

}

}

First, by @Autowired injection loadbalancerclient An instance of Client,client.choose () is the name of the service that needs to be called, and a service instance can be created after passing in. The eureka-client "Hello" interface is accessed through the instance's method call and returns the string information. Start Eureka-server, Eureke-client, Eureka-consumer three projects respectively, in the browser access http://localhost:1111/, from which you can see two services have been registered successfully

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
Next, open the new page access Http://localhost:1113/consumer, you can see the browser successfully output "Hello" string.

Springcloud MicroServices Series Tutorials (ii) Service registration and Discovery components Eureka
This shows that we have successfully consumed the Eureka-client service through the interface provided by Eureka-consumer.

Summary

This article gives you a brief introduction to the service Registration Discovery component Eureka instances, but also to achieve the service between the call, although the example is simple, but also a good head, the next series of tutorials will continue to explain the use of other components, welcome readers can learn with me, if the article has errors or shortcomings, Readers can point out in the comment area that I will actively give a reply!

Service registration and Discovery component Eureka Springcloud Micro Service series

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.