资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用SpringBoot如何实现注册与发现服务

这篇文章将为大家详细讲解有关使用SpringBoot如何实现注册与发现服务,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

专注于为中小企业提供成都网站制作、成都网站建设、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业两当免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

微服务

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

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

访问 http://localhost:8761 发现服务已经在注册中心上注册

使用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如何实现注册与发现服务

关于使用SpringBoot如何实现注册与发现服务就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前题目:使用SpringBoot如何实现注册与发现服务
当前链接:http://cdkjz.cn/article/jopghh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220