欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot的服务注册与发现示例

程序员文章站 2024-02-26 23:28:40
微服务 实践“微服务”自然要学习如何做服务注册与发现 基于springboot来进行微服务的学习,自然选择了与之息息相关的springcloud;当然可以选择其他的...

微服务

实践“微服务”自然要学习如何做服务注册与发现

基于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、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

服务注册与发现

springcloud模块

spring-cloud-starter-eureka-server

工程module

  1. 服务注册中心
  2. 服务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/

访问 http://localhost:8761

SpringBoot的服务注册与发现示例

服务注册

创建服务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 的配置来访问该服务

访问 发现服务已经在注册中心上注册

SpringBoot的服务注册与发现示例

服务注册中心启用用户名密码

通过配置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/

SpringBoot的服务注册与发现示例

代码请移步 github参考地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。