Home > Java > javaTutorial > body text

Detailed explanation of examples of service registration and discovery of 'microservices'

Y2J
Release: 2017-05-12 09:58:09
Original
4152 people have browsed it

This article mainly introduces SpringBoot service registration and discovery examples, which has certain reference value. Interested friends can refer to it

Microservice

Practicing "microservices" naturally requires learning how to do service registration and discovery

Based on SpringBoot to learn microservices, it is natural to choose SpringCloud, which is closely related to it; of course, you can choose other technologies. , such as dubbo

, you can also use zookeeper to implement service registration and discovery. As for whether zookeeper is good or bad to implement this function, everyone has their own opinion

SpringCloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems.SpringCloud

SpringCloud includes Distributed/versioned configuration, Distributed/ versioned configuration and many other sub-projects.

  1. Distributed/versioned configuration

  2. Service registration and discovery

  3. Routing

  4. Service-to-service calls

  5. Load balancing

  6. ##Circuit Breakers

  7. Global locks

  8. Leadership election and cluster state

  9. ##Distributed messaging

Service registration and discovery

SpringCloud module

spring-cloud-starter-eureka-server


Project

module

    Service Registration Center
  1. Service module
Service Registration Center

Create discovery module and introduce spring-cloud-starter-eureka-server dependency in build.gradle

apply plugin: 'org.springframework.boot'

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
  }
}
repositories {
  mavenCentral()
}
dependencies {
  compile ('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
jar {
  baseName = 'discovery-bootcwenao'
}
Copy after login

Provide registration center service through annotation @EnableEurekaServer

/**
 * @author cwenao
 * @version $Id DiscoveryBootcwenaoApplication.java, v 0.1 2017-01-12 9:56 cwenao Exp $$
 */
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryBootcwenaoApplication {
  public static void main(String[] args) {
    new SpringApplicationBuilder(DiscoveryBootcwenaoApplication.class).web(true).run(args);
  }
}
Copy after login

application.yml configure eureka

properties

Access localhost:8761


##Service registration

Create the service module and introduce spring-cloud-starter-eureka in build.gradle

apply plugin: 'org.springframework.boot'
dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
  }
}

dependencies {
  compile('org.springframework.cloud:spring-cloud-starter-eureka')
  compile('org.springframework.cloud:spring-cloud-stream')
}
sourceSets {
  main {
    resources.srcDirs = ['src/main/resources', 'src/main/java']
    resources.includes = ['**/*.xml', '**/*.yml']
  }
}
jar {
  baseName = 'apigateway-bootcwenao'
}
Copy after login

Register the service through the annotation @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayBootcwenaoApplication {
  public static void main(String[] args) {
    SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);
  }
}
Copy after login

Configure eureka properties in application.yml

server:
 port: 10002
spring:
 application:
  name: apigateway
eureka:
 client:
  registerWithEureka: true
  fetchRegistry: true
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/
Copy after login

After the registration is completed, you can access the service through the configuration of spring.application.name

Visit localhost:8761 and find that the service has been registered on the registration center


Service registration center enables username and password

By configuring applicaiton.yml username and password

security:
 basic:
  enabled: true
 user:
  name: aa
  password: abcd
Copy after login

Configure service provider application.yml

eureka:
 instance:
  hostname: configserver
  prefer-ip-address: true
 client:
  registerWithEureka: true
  fetchRegistry: true
  service-url:
   defaultZone: http://aa:abcd@localhost:8761/eureka/
Copy after login

【Related recommendations】

1.

Java Free Video Tutorial

2. JAVA Tutorial manual

3. Comprehensive analysis of Java annotations

The above is the detailed content of Detailed explanation of examples of service registration and discovery of 'microservices'. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template