Using spring Cloud to build a service registration center

Source: Internet
Author: User
Tags zookeeper

We have already introduced Ali's distributed service framework in our previous blog Dubbo "Install zookeeper on Linux and some caveats" "A simple case that takes you to the Dubbo distributed framework", but the small partners should also see that Ali's Dubbo can not complete the work alone, We also need to use the open source framework on Apache Zookeeper (not required, but best practice is zookeeper), using zookeeper as the service registry to achieve a better distributed application. Unlike Dubbo, Spring Cloud is a one-stop, distributed framework that provides developers with the tools to quickly build common patterns in distributed systems (such as configuration management, service discovery, circuit breakers, intelligent routing, micro-proxies, control buses). The coordination of distributed systems has led to boilerplate patterns, and the use of spring cloud developers can quickly support services and applications that implement these patterns. They will work well in any distributed environment, including developers ' own laptops, bare metal data centers, and hosted platforms such as Cloud Foundry. So today I'd like to take a simple case with small partners to learn about spring Cloud.

OK, so this article I would like to introduce in spring Cloud using Eureka to build a service registry, and then to register services. Since spring Cloud builds distributed services on the basis of spring boot, reading this article requires a little bit of spring boot knowledge, and if small partners are not familiar with spring boot, refer to the "https:// Github.com/lenve/javaeetest ".
OK, so above is the basic knowledge you need to read this article, and we'll look at how to use the Eureka in spring Cloud to build a service registry.

The first thing I want to say is that spring cloud is not a thing, like Hadoop, Spring cloud also contains a lot of sub-projects, and the Eureka we're looking at today is just one of them. Eureka's function is a bit like the zookeeper we wrote earlier, it's a service governance component that includes a service registry, service registration, and discovery mechanisms. Other components We do not introduce here first, the following series of articles will be one by one mentions to.

OK, Nonsense said a bunch of, on the code bar.

Create a Service registration center

Create a normal spring boot project

First we need to create a normal spring boot project, named Eureka-server, to what extent? It is a starter that does not need to be added, and only a parent starter is referenced after a successful creation.

Add Eureka Dependency

After the project is successfully created, add the eureka-server dependency to the Pom.xml file, the current stable version of Eureka is DALSTON.SR3, and after the dependency is added, the Pom.xml file looks like this:

<?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>org.sang</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>demo Project for Spring boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>Dalston.SR3</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>
So here depends on the add I mainly refer to the Eureka official website of the http://projects.spring.io/spring-cloud/.

Start a Service registration center

The way to start a service registry is simply to add a @enableeurekaserver annotation to the Spring boot entry class as follows:

@EnableEurekaServer
@SpringBootApplication
public class Eurekaserverapplication {
public static void Main (string[] args) {
Springapplication.run (Eurekaserverapplication.class, args);
}
}
Configure the Service registration center

Finally, we'll do a little more simple configuration, the configuration is written in the Spring boot configuration file application.properties, the following wording:

server.port=1111
Eureka.instance.hostname=localhost
Eureka.client.register-with-eureka=false
Eureka.client.fetch-registry=false
eureka.client.service-url.defaultzone=http://${eureka.instance.hostname}:${server.port}/eureka/
OK, so for the comments on these lines, I'll say the following points:

1.SERVER.PORT=1111 indicates the port number of the service registry is set
2.eureka.instance.hostname=localhost indicates that the service registry is set up hostname
3.eureka.client.register-with-eureka=false, because the app we're currently creating is a service registry, not an ordinary app, by default, the app registers itself with the registry (and itself), Set to False to disallow this default behavior
4.eureka.client.fetch-registry=false, indicating that no other service is to be retrieved because the service registry itself is responsible for maintaining the service instance, and it does not need to retrieve other services

Test

OK, after all this, we can start this spring boot service, after the service starts successfully, enter in the browser: http://localhost:1111 will be able to see the following page:

OK, after seeing this page, your service registry has been set up.

Summary

We have previously dedicated a blog about how to install zookeeper "Linux install Zookeeper and some considerations" on Linux, but there is no such problem with Eureka because the service registry in Eureka is actually a spring The boot project, and the Spring boot project we know can be directly into a jar package, and then the Java-jar command can be run, regardless of Windows or Linux, the mode of operation is consistent.

Registered service Provider

OK, so now that the service registry has been available, we can consider registering a service provider with the Service registration center.

Create a new spring boot project

Or create a spring boot project, this time create one more step than before, at the time of creation, select the Web starter, we create a Web project, select the web when created in IntelliJ idea, as follows:

Add Eureka Dependency

In creating a good project, we need to add Eureka dependencies, which are added in the following ways:

<?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>org.sang</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>provider</name>
<description>demo Project for Spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</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>
Create an app's portal

This is a Web project, so we add a controller that provides an access entry in the controller, as follows:

@RestController
public class Hellocontroller {
Private final Logger Logger = Logger.getlogger (GetClass ());
@Autowired
Private Discoveryclient client;

@RequestMapping (value = "/hello", method = Requestmethod.get)
Public String Index (www.zzktv.cn) {
list<serviceinstance> instances = client.getinstances ("Hello-service");
for (int i = 0; i < instances.size (www.7881188.cn); i++) {
Logger.info ("/hello,host:" + instances.get (i). GetHost () + ", service_id:" + instances.get (i). Getserviceid ());
}
Return "Hello World";
}
}
After the service is created, the service-related information is printed in the log.

Activating the Discoveryclient in Eureka

At the entry function of spring boot, Activate the discoveryclient implementation in Eureka by adding @enablediscoveryclient annotations (because we injected discoveryclient in Hellocontroller).

@EnableDiscoveryClient
@SpringBootApplication
public class www.lgzxyl.com Providerapplication {

public static void Main (string[www.wmyl999.cn] args) {
Springapplication.run (Providerapplication.class, args);
}
}
Configure Service name and registry address

Finally, we configure the service name and the registry address in the Application.properties file as follows:

Spring.application.name=hello-service
Eureka.client.service-url.www.thd540.com Defaultzone=http://localhost:1111/eureka
The meaning of these two lines of code is very simple, I will not say more.

Test

After all this, we can test, directly run the Spring boot project, after the successful operation, we refresh just http://localhost:1111, we can see a service has been registered successfully. As follows:

At the same time, we look at this service provider run log, you can also see the service information, as follows:

Summary

OK, so after that, one of our service registries was built successfully, and a service provider successfully registered. However, there is a small problem, that is, we are a single-node service registry, once the failure of the entire service is paralyzed, so in practical applications, we need to build a highly available registry, then about the high-availability registry How to build the problem, we will be in the next article to introduce.

Using spring Cloud to build a service registration center

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.