Eureka Service management - to build simple applications

Create a registry server Eureka

The key dependent:

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

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

yml profile: 

server:
  port: 9100  #端口号

eureka:
  instance:
    hostname: localhost #注册中心地址
  client:
    register-with-eureka: false #是否将自己注册到注册中心
    fetch-registry: false #是否从注册中心检索服务
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Use the startup class @EnableEurekaServer comment open registry services:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

Start the application to access your browser to http: // localhost: 8100:

Registration Service

Creating a user service app-user, registered to Eureka registry.

The key dependent:

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

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

yml profile:

server:
  port: 8010

spring:
  application:
    name: app-user  #服务别名-注册到注册中心之后的名称

eureka:
  client:
    register-with-eureka: true  #注册到注册中心
    fetch-registry: true  #检索其他服务
    serviceUrl:
      defaultZone: http://localhost:9100/eureka/  #注册目标地址


Use the startup class @EnableEurekaClient registered user registry service to Eureka:

@SpringBootApplication
@EnableEurekaClient
public class UserServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServerApplication.class, args);
    }

}

Open again registry page http: // localhost: 8100, you can see app-user services have been registered came

Published 46 original articles · won praise 0 · Views 2026

Guess you like

Origin blog.csdn.net/hon_vin/article/details/102798068
Recommended