本篇文章主要介紹了SpringBoot的服務註冊與發現範例,具有一定的參考價值,有興趣的小夥伴們可以參考一下
微服務
實踐「微服務」自然要學習如何做服務註冊與發現
基於SpringBoot來進行微服務的學習,自然選擇了與之息息相關的SpringCloud;當然可以選擇其他的技術進行,例如dubbo
也可以用zookeeper來實現服務註冊與發現,至於zookeeper來實現此功能好還是不好,各家之言都有
#SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems .SpringCloud
SpringCloud 包含了Distributed/versioned configuration、
SpringCloud 包含了Distributed/versioned configuration、
Distributed messaging
SpringCloud模組spring-cloud-starter-eureka-server
工程
服務註冊中心
服務module
##服務註冊中心
建立discovery module,並在build.gradle中引入spring-cloud-starter-eureka-server依賴
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' }
/** * @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); } }
server: port: 8761 eureka: instance: hostname: discovery client: registerWithEureka: false fetchRegistry: false service-url: defaultZone: http://discovery:${server.port}/eureka/
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' }
@SpringBootApplication @EnableDiscoveryClient public class ApiGatewayBootcwenaoApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args); } }
server: port: 10002 spring: application: name: apigateway eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:8761/eureka/
security: basic: enabled: true user: name: aa password: abcd
eureka: instance: hostname: configserver prefer-ip-address: true client: registerWithEureka: true fetchRegistry: true service-url: defaultZone: http://aa:abcd@localhost:8761/eureka/
以上是「微服務」之服務註冊與發現的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!