springcloud essay 2 -- eureka

Through the previous explanation, everyone has some understanding of the simple application of springboot, but in actual work, each business division and product line are maintaining their own projects, and rely on the projects of other product lines, then, each project's How to maintain interdependence and invocation efficiently and concisely? It's time to let the concept of service registration and discovery come into play: the service discovery mechanism refers to the service provider's information obtained by the service consumer through this mechanism, even if the service provider's Configuration or information changes are imperceptible to consumers, or low intrusive.
Let's briefly talk about the relationship between the service provider, the service consumer and the registry (that is, the service discovery component)

  1. When each service is started, it will register its own configuration information to the registration center, and the registration center will save this information for other services to call. The services mentioned here include service providers, service consumers, and registration centers. yourself (this will be explained later)
  2. The service consumer does not know the network information of the service provider, but first obtains the provider's network address through the registration center, and then calls the service
  3. Each service communicates with the registry through a certain mechanism (such as heartbeat check), indicating that its own service is alive. If a service cannot communicate with the registry for a long time, the registry will cancel the instance.
  4. After the service address changes, it will re-register with the registry. Consumers do not need to manually modify the provider's network address and other information

Eureka is the registry component in the springcloud family, which is itself a rest service, including server and client.
Let 's write a simple Eureka Server

buildscript {
    repositories {
        mavenLocal()
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
    }
}
apply plugin: 'application'
apply plugin: 'org.springframework.boot'



def env = System.getProperty("env") ?: "dev"

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources", "src/main/profile/$env"]
        }
    }
}


dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Edgware.SR1"
    }
}
dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}

Configuration file application-peer1.yml

server:
  port: 6201
eureka:
  instance:
    prefer-ip-address: true
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:6202/eureka
    register-with-eureka: true
    fetch-registry: true

spring:
  application:
    name: miracle-eureka-server

logging:
  config: classpath:log4j2.xml

startup class

package com.miralcle.eureka;

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

/**
 * Created by naonao on 18/4/22.
 */
@SpringBootApplication
@EnableEurekaServer
public class MiracleEurekaApplication1 {
    public static void main(String[] args) {
        setProfile("peer1");
        SpringApplication.run(MiracleEurekaApplication1.class, args);

    }
    public static void setProfile(String active) {
        String profile = "spring.profiles.active";
        System.setProperty(profile, active);
    }
}

Writing a MiracleEurekaApplication2

package com.miralcle.eureka;

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

/**
 * Created by naonao on 18/4/22.
 */
@SpringBootApplication
@EnableEurekaServer
public class MiracleEurekaApplication2 {
    public static void main(String[] args) {
        MiracleEurekaApplication1.setProfile("peer2");
        SpringApplication.run(MiracleEurekaApplication2.class, args);
    }
}

The corresponding configuration file is similar to peer1, the specific code can be seen

Start two applications and enter licalhost:6201 to view

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731821&siteId=291194637