源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

SpringBoot的服务注册与发现示例

  • 时间:2020-04-11 22:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:SpringBoot的服务注册与发现示例
[b]微服务[/b] 实践“微服务”自然要学习如何做服务注册与发现 基于SpringBoot来进行微服务的学习,自然选择了与之息息相关的SpringCloud;当然可以选择其他的技术进行,比如dubbo 也可以用zookeeper来实现服务注册与发现,至于zookeeper来实现此功能好还是不好,各家之言都有 [b]SpringCloud[/b] Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems .[url=http://projects.spring.io/spring-cloud/]SpringCloud[/url] SpringCloud 包含了 Distributed/versioned configuration、Distributed/versioned configuration等很多子项目。 [list=1] [*]Distributed/versioned configuration[/*] [*]Service registration and discovery[/*] [*]Routing[/*] [*]Service-to-service calls[/*] [*]Load balancing[/*] [*]Circuit Breakers[/*] [*]Global locks[/*] [*]Leadership election and cluster state[/*] [*]Distributed messaging [/*] [/list] [b]服务注册与发现[/b] SpringCloud模块 spring-cloud-starter-eureka-server 工程module [list=1] [*]服务注册中心[/*] [*]服务module[/*] [/list] [b]服务注册中心[/b] 创建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/
访问 http://localhost:8761 [img]http://files.jb51.net/file_images/article/201705/2017050216491137.jpg[/img] [b]服务注册[/b] 创建服务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 的配置来访问该服务 访问 [url=http://localhost:8761/]http://localhost:8761[/url] 发现服务已经在注册中心上注册 [img]http://files.jb51.net/file_images/article/201705/2017050216491138.jpg[/img] [b]服务注册中心启用用户名密码[/b] 通过配置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/
[img]http://files.jb51.net/file_images/article/201705/2017050216491239.jpg[/img] 代码请移步 [url=https://github.com/cwenao/springboot_cwenao]Github参考地址[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部