Eureka registry - to build simple Eureka environment

Introduction 1. Eureka

        Eureka the ancient Greek word vocabulary, speech is interjection, meaning "I found it! I found it!." According to legend, Archimedes discovered the principle of buoyancy while taking a bath, happy time to put on pants, ran into the street shouting: "! Eureka (I found)."

       In architecture now micro services in will have a registry, because of the large number of micro-services, to make remote calls between micro-services need to know the server's ip and port, the registry can manage these services address and port; in addition, micro service will immediately report its status to the registry, the registry unified management status of these services.

        Spring Cloud Eureka is an implementation of the technical service governance to achieve a functional service governance. Eureka to provide services and client, the server that is Eureka registry itself, and the client to complete the service registration and discovery to micro Eureka registry.

 

2. build simple Eureka Service

2.1 Creating spring boot project

        Create a springboot created by way of general springboot project to project.

2.2 involves dependence

       Use eureka spring as the registry needs to rely on the cloud is dependent on the eureka. In the actual project, spring cloud depends on the will of the Father euraka engineering project (parent) in.

<!-- spring cloud -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-dependencies</artifactId>
	<version>Edgware.SR4</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>

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

 2.3 startup class

        Eureka projects do not need to write code, but to modify the spring boot configuration with the startup class yml file, as shown in the following code.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer    // 标示该工程为eurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2.4 application.yml modify configuration files

server:
  port: 8761  # 端口

spring:
  application:
    name: cloud-demo-eureka # 服务名

eureka:
  server:
    enable-self-preservation: false # 是否开启自我保护模式
    eviction-interval-timer-in-ms: 60000  # 服务注册清理间隔时间(单位:毫秒),表示每隔一分钟清理没有上报状态的服务
  client:
    register-with-eureka: false  # 服务注册,是否将自己这一个服务注册到Eureka
    fetch-registry: false # 是否从Eureka获取注册信息
    service-url:  # Eureka客户端与服务端交互的地址(可以在服务启动后,通过浏览器进入查看服务注册信息)
    defaultZone: http://127.0.0.1:8761/eureka/

2.5 Start Service

        Running spring boot startup class, after the successful launch, the value can be configured in application.yml defaultZone by accessing, viewing detailed information registry run.

http://127.0.0.1:8761/euraka/

 

3. Customer service registration to Eureka

3.1 introduces dependence

        Eureka will need to sign up to the service registry to rely Eureka eureka client's need to import.

 

<!-- eureka客户端依赖 -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2 added eureka client configuration

        Eureka configure client in need to register to eureka services yml configuration file, the following reference configuration:

eureka:
  client:
    registerWithEureka: true  # 服务注册开关
    fetchRegistry: true # 服务发现开关
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
  instance:
    preferIpAddress: true # 将本服务地址注册到eureka
    instance-id: ${spring.cloud.client.name}:${server.port} # 实例id

3.3 startup class add comments

        On eureka need to register to add comments startup class of service to indicate it is registered to a eureka services, and other services can be found on the eureka.

@EnableDiscoveryClient
@EnableEurekaClient

3.4 Start Service

        After finished configuration, start eureka service side of the service, and then start the client services, the completion of the project started, and you can view the details of the current instance of the registry in a browser, the default address is:

http://127.0.0.1:8761/euraka/

 

Published 48 original articles · won praise 52 · views 20000 +

Guess you like

Origin blog.csdn.net/y506798278/article/details/97821991