Home >Java >javaTutorial >Detailed explanation of examples of service registration and discovery of 'microservices'
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.
Distributed/versioned configuration
Service registration and discovery
Routing
Service-to-service calls
Load balancing
SpringCloud module
spring-cloud-starter-eureka-server
Project
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' }
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); } }
application.yml configure eureka
propertiesserver:
port: 8761
eureka:
instance:
hostname: discovery
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://discovery:${server.port}/eureka/
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' }Register the service through the annotation @EnableDiscoveryClient
@SpringBootApplication @EnableDiscoveryClient public class ApiGatewayBootcwenaoApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args); } }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/After the registration is completed, you can access the service through the configuration of spring.application.nameVisit 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: abcdConfigure 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/
【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!