欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例

 更新時間:2017年05月02日 16:59:40   作者:代碼行間的無聊生活  
本篇文章主要介紹了SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

微服務(wù)

實踐“微服務(wù)”自然要學(xué)習(xí)如何做服務(wù)注冊與發(fā)現(xiàn)

基于SpringBoot來進(jìn)行微服務(wù)的學(xué)習(xí),自然選擇了與之息息相關(guān)的SpringCloud;當(dāng)然可以選擇其他的技術(shù)進(jìn)行,比如dubbo

也可以用zookeeper來實現(xiàn)服務(wù)注冊與發(fā)現(xiàn),至于zookeeper來實現(xiàn)此功能好還是不好,各家之言都有

SpringCloud

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

SpringCloud 包含了 Distributed/versioned configuration、Distributed/versioned configuration等很多子項目。

  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

服務(wù)注冊與發(fā)現(xiàn)

SpringCloud模塊

spring-cloud-starter-eureka-server

工程module

  1. 服務(wù)注冊中心
  2. 服務(wù)module

服務(wù)注冊中心

創(chuàng)建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 提供注冊中心服務(wù)

/**
 * @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/

訪問 http://localhost:8761


服務(wù)注冊

創(chuàng)建服務(wù)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 進(jìn)行服務(wù)注冊

@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 的配置來訪問該服務(wù)

訪問 http://localhost:8761 發(fā)現(xiàn)服務(wù)已經(jīng)在注冊中心上注冊


服務(wù)注冊中心啟用用戶名密碼

通過配置applicaiton.yml用戶名密碼

security:
 basic:
  enabled: true
 user:
  name: aa
  password: abcd

配置服務(wù)提供方application.yml

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

代碼請移步 Github參考地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 客戶端設(shè)置超時時間真的很重要

    客戶端設(shè)置超時時間真的很重要

    今天小編就為大家分享一篇關(guān)于客戶端設(shè)置超時時間真的很重要,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 新手入門學(xué)習(xí)Spring Freemarker教程解析

    新手入門學(xué)習(xí)Spring Freemarker教程解析

    這篇文章主要介紹了新手入門學(xué)習(xí)Freemarker教程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • java正則表達(dá)式判斷 ip 地址是否正確解析

    java正則表達(dá)式判斷 ip 地址是否正確解析

    這篇文章主要介紹了java正則表達(dá)式判斷 ip 地址是否正確解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Java 并發(fā)編程之ThreadLocal詳解及實例

    Java 并發(fā)編程之ThreadLocal詳解及實例

    這篇文章主要介紹了Java 并發(fā)編程之ThreadLocal詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java?OpenCV圖像處理之自定義圖像濾波算子

    Java?OpenCV圖像處理之自定義圖像濾波算子

    這篇文章主要為大家介紹了如何利用Java?OpenCV實現(xiàn)自定義圖像濾波(降噪)?算子,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下
    2022-02-02
  • Java字符串逆序方法詳情

    Java字符串逆序方法詳情

    這篇文章主要介紹了Java字符逆序,字符逆序主要原理就是將一個字符串str的內(nèi)容顛倒過來,并輸出,下文操作分享需要的小伙伴可以參考一下
    2022-03-03
  • 淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    Spring為大家內(nèi)置了不少開箱即用的轉(zhuǎn)換類,如字符串轉(zhuǎn)數(shù)字、字符串轉(zhuǎn)時間等,但有時候需要使用自定義的屬性,則需要自定義轉(zhuǎn)換類了
    2021-06-06
  • Java?SimpleDateFormat與System類使用示例詳解

    Java?SimpleDateFormat與System類使用示例詳解

    這篇文章主要介紹了Java?SimpleDateFormat與System類使用示例,對于SimpleDateFormat類,是一個用來區(qū)分區(qū)域設(shè)置的方式進(jìn)行日期的是指,以及對日期進(jìn)行處理分析的一個實現(xiàn)類
    2022-11-11
  • MyBatis多表查詢和注解開發(fā)案例詳解

    MyBatis多表查詢和注解開發(fā)案例詳解

    這篇文章主要介紹了MyBatis多表查詢和注解開發(fā),本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • 使用Feign配置請求頭以及支持Https協(xié)議

    使用Feign配置請求頭以及支持Https協(xié)議

    這篇文章主要介紹了使用Feign配置請求頭以及支持Https協(xié)議,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論