本篇文章主要介紹了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' }透過註解@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 設定eureka屬性
server: port: 8761 eureka: instance: hostname: discovery client: registerWithEureka: false fetchRegistry: false service-url: defaultZone: http://discovery:${server.port}/eureka/存取localhost:8761 ######## #####建立服務module, 在build.gradle中引入spring-cloud-starter-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' }###透過註解@EnableDiscoveryClient 進行服務註冊###
@SpringBootApplication @EnableDiscoveryClient public class ApiGatewayBootcwenaoApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args); } }###application.yml 設定eureka屬性# ##
server: port: 10002 spring: application: name: apigateway eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:8761/eureka/###註冊完成後,可以透過spring.application.name 的設定來存取該服務######存取localhost:8761 發現服務已經在註冊中心上註冊########## ############服務註冊中心啟用使用者名稱密碼#########透過設定applicaiton.yml使用者名稱密碼###
security: basic: enabled: true user: name: aa password: abcd###設定服務提供者application.yml ###
eureka: instance: hostname: configserver prefer-ip-address: true client: registerWithEureka: true fetchRegistry: true service-url: defaultZone: http://aa:abcd@localhost:8761/eureka/############【相關推薦】######1. ###Java免費影片教學#########2. ###JAVA教程手冊#########3. ###全面解析Java註解######
以上是「微服務」之服務註冊與發現的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!