一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例

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

2020-09-22 10:39代碼行間的無聊生活 Java教程

本篇文章主要介紹了SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

微服務(wù)

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

基于SpringBoot來進行微服務(wù)的學(xué)習(xí),自然選擇了與之息息相關(guān)的SpringCloud;當(dāng)然可以選擇其他的技術(shù)進行,比如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依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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ù)

?
1
2
3
4
5
6
7
8
9
10
11
/**
 * @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屬性

?
1
2
3
4
5
6
7
8
9
10
server:
 port: 8761
eureka:
 instance:
  hostname: discovery
 client:
  registerWithEureka: false
  fetchRegistry: false
  service-url:
   defaultZone: http://discovery:${server.port}/eureka/

訪問 http://localhost:8761

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

服務(wù)注冊

創(chuàng)建服務(wù)module, 在build.gradle中引入 spring-cloud-starter-eureka

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 進行服務(wù)注冊

?
1
2
3
4
5
6
7
@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayBootcwenaoApplication {
  public static void main(String[] args) {
    SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);
  }
}

application.yml 配置eureka屬性

?
1
2
3
4
5
6
7
8
9
10
11
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)在注冊中心上注冊

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

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

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

?
1
2
3
4
5
6
security:
 basic:
  enabled: true
 user:
  name: aa
  password: abcd

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

?
1
2
3
4
5
6
7
8
9
eureka:
 instance:
  hostname: configserver
  prefer-ip-address: true
 client:
  registerWithEureka: true
  fetchRegistry: true
  service-url:
   defaultZone: http://aa:abcd@localhost:8761/eureka/

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

代碼請移步 Github參考地址

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

原文鏈接:http://www.jianshu.com/p/db45c87e72b6

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: gay18高中生白袜xnxx动漫 | 日本一道一区二区免费看 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 男女爆操 | 亚洲精品成人A8198A片漫画 | 四虎导航 | 天天色踪合合 | 四虎影视永久在线 | 精品久久久久久影院免费 | 国产成人一区二区三区在线视频 | 香蕉eeww99国产精选播放 | 明星梦淫 | 日本手机在线 | 波多野结衣小说 | 99久久国产综合精品网成人影院 | 国产精品高清视亚洲一区二区 | 青青在线国产视频 | 二次元美女脱裤子让男人桶爽 | 青青草国产一区二区三区 | 婷婷在线观看香蕉五月天 | bt天堂在线最新版在线 | 青青草原免费在线视频 | 日本三级成人中文字幕乱码 | 国产亚洲精品第一综合linode | 欧美视频一区二区专区 | 色综合合久久天天综合绕视看 | 国产性做久久久久久 | 欧美jjvideo| 天堂a视频| 国产精品制服丝袜白丝www | 国内精品视频一区二区三区八戒 | 黄a一级| 国产91对白在线观看 | 日本视频在线播放 | 爽爽窝窝午夜精品一区二区 | 国产精品麻豆免费版 | 亚洲酒色1314狠狠做 | 日本在线视频免费观看 | 把内裤拔到一边高h1v1 | 日本午夜大片免费观看视频 | 香蕉成人国产精品免费看网站 |