Spring Cloud's method for building Eureka applications, cloudeureka

Source: Internet
Author: User

Spring Cloud's method for building Eureka applications, cloudeureka

Eureka Introduction

Eureka provides REST-based services and is mainly used for service management in clusters. Eureka provides a Java-based client component that implements the load balancing function and creates conditions for cluster deployment of business components. With this framework, you can register business components to the Eureka container. These business components can be deployed in clusters. Eureka mainly maintains the list of these services and automatically checks their statuses.

Program Structure

Create Eureka Server

Maven dependency

  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Dalston.SR1</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>  <dependencies>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency>  </dependencies>

Modify the spring boot port in application. yml

server: port: 8761

Enable Eureka service annotation @ EnableEurekaServer

@EnableEurekaServer@SpringBootApplicationpublic class EKServerApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EKServerApplication.class).run(args);  }}

Start springboot

[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context[main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)[main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761[main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)

During startup, an exception occurs that cannot be connected to the server. This is because Eureka uses itself as a client to capture registration information from the server during startup.

Copy codeThe Code is as follows:
Com. netflix. discovery. shared. transport. TransportException: Cannot execute request on any known server

The following configuration will not cause this exception again during startup:

eureka: client:  registerWithEureka: false  fetchRegistry: false

RegisterWithEureka declares whether to register its information to the Eureka server. The default value is true.

FetchRegistry indicates whether to capture registration information on the Eureka server. The default value is true.

Access http: // localhost: 8761 in the browser to view the Eureka console and enter the image description

Create a service provider

Dependency

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

Configure the port, Eureka Instance name, And Eureka service address in application. yml.

server: port: 8080spring: application:  name: ek-providereureka: instance:  hostname: localhost client:   serviceUrl:    defaultZone: http://localhost:8761/eureka/

Create a REST Service

@RestControllerpublic class HelloController {  @RequestMapping("/hello")  public String hello(HttpServletRequest request) {    return "hello:" + request.getRequestURL();  }}

Enable Eureka client annotation @ EnableEurekaServer

@EnableEurekaClient@SpringBootApplicationpublic class EkProviderApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EkProviderApplication.class).run(args);  }}

After the service is started, you can see on the Eureka console that the service provider has registered in Eureka.

Create a service caller

Dependency

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

Configure the port, Eureka Instance name, And Eureka service address in application. yml.

server: port: 9000spring: application:  name: ek-invokeeureka: instance:  hostname: localhost client:   serviceUrl:    defaultZone: http://localhost:8761/eureka/

Compile a "/hello" API for calling a REST service"

@RestController@Configurationpublic class InvokeController {  @Bean  @LoadBalanced  public RestTemplate getRestTemplate() {    return new RestTemplate();  }  @RequestMapping("/invoke")  public String invoke() {    RestTemplate restTemplate = getRestTemplate();    return restTemplate.getForObject("http://ek-provider/hello", String.class);  }}

In the traditional mode, we usually use Httpclient in Apache to call the REST service. Here we use Spring to provide the RestTemplate component that calls the REST service. RestTemplate itself does not have the ability to call distributed services, but after the RestTemplate bean is modified by the @ LoadBalanced annotation, this RestTemplate instance has the ability to access distributed services, this is due to the various interceptors provided by Spring for it.

Enable Eureka client annotation @ EnableEurekaServer

@EnableEurekaClient@SpringBootApplicationpublic class EkInvokeApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EkInvokeApplication.class).run(args);  }}

After the service is started, you can see on the Eureka console that the Service caller has registered in Eureka.

Then access the "invoke" interface of the Service caller in the browser and return the following

Summary

The Eureka server maintains the latest registration information through the heartbeat link, which is stored in the memory.

Eureka service providers mainly perform the following operations:

  1. Register a service with the Eureka Server
  2. Send heartbeat to the Eureka server to keep the registration information of the Eureka server up-to-date.
  3. Obtain the latest registration list from the server. Generally, the Eureka client is both a service provider and a service caller, but its primary responsibility is to provide services.

Eureka service callers mainly perform the following operations:

  1. Register a service with the Eureka Server
  2. Send heartbeat to the Eureka server to keep the registration information of the Eureka server up-to-date.
  3. Obtain the latest registration list from the server. Generally, the Eureka client is both a service provider and a service caller, but its main responsibilities are discovery and call.

Source Code address: https://github.com/xc564864894/springcloud/tree/master/Eureka (% E4 % B8 % 80)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.