php调取java微服务_“微服务”之服务注册与发现的实例详解

本文详细讲解了如何使用SpringBoot和SpringCloud搭建服务注册与发现的示例,包括创建Eureka服务注册中心,服务提供方的注册,以及启用Eureka的用户名密码认证。通过对`@EnableEurekaServer`和`@EnableDiscoveryClient`注解的使用,实现了服务的注册和发现功能。
摘要由CSDN通过智能技术生成

本篇文章主要介绍了SpringBoot的服务注册与发现示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

微服务

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

基于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等很多子项目。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

服务注册与发现

SpringCloud模块

spring-cloud-starter-eureka-server服务注册中心

服务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/

访问 localhost:8761

f38a518259f05cae22b9f0e6855c190c.png

服务注册

创建服务module, 在build.gradle中引入 spring-cloud-starter-eurekaapply 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 的配置来访问该服务

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

7f0be188cc0218603fa6c81380e15d78.png

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

通过配置applicaiton.yml用户名密码security:

basic:

enabled: true

user:

name: aa

password: abcd

配置服务提供方application.ymleureka:

instance:

hostname: configserver

prefer-ip-address: true

client:

registerWithEureka: true

fetchRegistry: true

service-url:

defaultZone: http://aa:abcd@localhost:8761/eureka/

222db84e07519d1abd99d2789fc002d1.png

【相关推荐】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值