Micro-service building based on spring cloud-3 service governance: Spring Cloud Eureka

Source: Internet
Author: User

Learning based on Spring Cloud's microservices-3 service governance: Spring Cloud Eureka what is service governance

Service governance can be said to be the most core and basic module in MicroServices architecture, which is mainly used to realize the automatic registration and discovery of each micro-service instance. Why service governance modules are needed

There may not be much service when you initially build a microservices system, and we can do some static configuration to complete the service invocation

At this point, everything is still normal.

As the project nears its end, maintenance personnel need to maintain more and more services, more and more complex, and eventually form a large number of configuration files, maintenance will become more and more difficult. At this point, the Automation management framework for MicroServices application instances becomes critical. What tasks the Service governance framework needs to accomplish

    • Service registration: In the service governance framework, a registry is typically built, each service unit registers its own services with the registry, informs the Registry of additional information such as host and port number, version number, communication protocol, and so on, and the registry organizes the list of services by service name.

    • Service discovery: All of our services are registered with the registry and are categorized by service name in the registry, and are serviced by the registry maintainer in a specific location. So when a caller needs to invoke a service, it needs to consult with the registry, which returns all the specific locations of the callee service, and the caller chooses a specific location for the service invocation based on some kind of polling policy.

Netflix Eureka

Spring Cloud Eureka, using Netflix Eureka for service registration and discovery, contains both the server-side components and the client components. Eureka Service Side

Eureka Server, which we also call the service registry, supports highly available configurations like other service registries. It relies on strong consistency to provide good service instance availability and can handle a variety of different failure scenarios.

If Eureka is deployed in a clustered manner, when there are shards in the cluster that fail, then the Eureka goes into self-protection mode. It allows the discovery and registration of services to continue during a shard failure, and when a failed shard resumes running, the other shards in the cluster will synchronize their status back again. Eureka Client

Eureka Client, mainly handles the registration and discovery of services. The client service is embedded in the code of the client application through annotations and parameter configuration, and when the application runs, the Eureka client registers its own service with the registry and periodically sends a heartbeat to update its service lease. At the same time, he can query the current registered service information from the server and cache them locally and refresh the service status periodically. Server-to-client relationship

Build a Service registration center

1. Create the Spring boot project, named Eureka-server, and include the necessary dependencies in the Pom, such as:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!--lookup parent from repository to
</parent>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependencymanagement>
     <dependencies>
          <dependency>
              <groupid>org.springframework.cloud</groupid>
              <artifactid>spring-cloud-dependencies</artifactid>
              <version>Camden.SR7</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </ Dependencies>
</dependencymanagement>

2. Start a service registry with @enableeurekaserver annotations to make conversations with other apps. This feature can be turned on by adding this annotation to the spring boot app.

@EnableEurekaServer
@SpringBootApplication
public class Eurekaserverapplication {


public static void Main (string[] args) {

New Springapplicationbuilder (Eurekaserverapplication.class). Web (True). Run (args);
}
}

By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by simply adding the following configuration in Application.properties:

Spring.application.name=eureka-server
server.port=1111
Eureka.instance.hostname=localhost
Eureka.client.register-with-eureka=false
Eureka.client.fetch-registry=false
eureka.client.serviceurl.defaultzone=http://${eureka.instance.hostname}:${server.port}/eureka/

    • Spring.application.name is the service name, and the value of this property is seen in the service registry, and is the name used when calling between services.
    • Server.port is the port number used by the service when it starts.
    • Eureka.instance.hostname This is a custom parameter.
    • Eureka.client.register-with-eureka because the app is a registry, it is set to false, and the representative does not register itself with the service registry.
    • Eureka.client.fetch-registry because the service registry is responsible for maintaining the service instance, it does not need to retrieve the service, so it is set to false.

Complete the above configuration, enter http:localhost:1111/in the browser,

At this point the instances currently registered with Eureka Bar is empty because there is no service registered to the registry.

Registered service Provider (Eureka client)

We can join the Eureka Service governance system directly using the spring boot application created in the previous chapter.

1. Modify the pom.xml to increase the reliance of the Spring Cloud Eureka module. As follows:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2. Modify the contents of the Hellocontroller class to print the service in the log:

@RestController
public class hellocontroller{

Private final Logger Logger = Logger.getlogger (GetClass ());
@Autowired
Private Discoveryclient client;

@RequestMapping (value= "/hello", method = Requestmethod.get)
Public String Index () {
serviceinstance instance = Client.getlocalserviceinstance ();
Logger.info ("/hello, Host:" + instance.gethost () + ", service_id:" + Instance.getserviceid ());
Return "Hello World";
}
}

3. Add @enablediscoveryclient annotations in the main class to activate the Discoveryclient implementation in Eureka (automated configuration, Creating an Discoveryclient interface for eurekadiscoveryclient instances of Eureka clients)

@EnableDiscoveryClient
@SpringBootApplication

@ComponentScan ("Com.microservice.web")//quotation mark to fill in the controller's package name
public class Springbootapplication {

public static void Main (string[] args) {
Springapplication.run (Springbootapplication.class, args);
}
}

4. Modify the Application.properties file:

Spring.application.name=hello-service
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

    • The Eureka.client.serviceUrl.defaultZone property specifies the address of the service registry.

5. Start the Service registration center and the Hello-service service separately. Results such as:

By accessing Localhost:8080/hello, a request is made directly to the service, which can be seen in the console as shown in the following:

The content of these outputs is the Discoveryclient interface object we inject in the controller, and the service-related information obtained from the service registry.

Micro-service building based on spring cloud-3 service governance: Spring Cloud Eureka

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.