Spring Cloud注册中心与服务发现

版本信息

Spring Cloud : Hoxton.SR1
Spring Boot : 2.2.2.RELEASE
Zookeeper : 3.5.6
Consul : 1.6.3
Nacos: 1.1.4

核心api

  • org.springframework.cloud.client.serviceregistry.ServiceRegistry 服务注册
    - org.springframework.cloud.client.serviceregistry.Registration 注册信息
    - org.springframework.cloud.client.ServiceInstance 服务实例信息

服务注册只能单注册中心,不能多注册中心

  • org.springframework.cloud.client.discovery.DiscoveryClient 服务发现
    - org.springframework.cloud.client.ServiceInstance 服务实例信息

默认实现:org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient,可以多种服务发现并行

基于Euraka的注册中心以及服务发现

服务端
应用
1.添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--eureka注册中心-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.配置文件application.yml

spring:
  application:
    name: euraka-server
server:
  port: 9090

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:${
   server.port}/eureka/

3.启动类EurekaDiscoveryServerApplication

/**
 * Eureka 服务发现注册中心
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/2/1
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaDiscoveryServerApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(EurekaDiscoveryServerApplication.class, args);
    }
}

Eureka 服务端访问地址: http://127.0.0.1:9090/

Spring Cloud Eureka
服务端配置类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean

Eureka 服务端端点:
https://github.com/Netflix/eureka/wiki/Eureka-REST-operations

Spring Cloud Eureka 服务端 端点去掉版本信息即可,比如/eureka/v2/apps改成/eureka/apps

源码分析

  • 分析@EnableEurekaServer
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
   

}

@Configuration(proxyBeanMethods = false)
public class EurekaServerMarkerConfiguration {
   

	@Bean
	public Marker eurekaServerMarkerBean() {
   
		return new Marker();
	}

	class Marker {
   

	}

}

导入了EurekaServerMarkerConfiguration配置信息,该配置注册了Marker的Bean,由前面的@EnableConfigServer得知,这个Marker的Bean是一个类EurekaServerAutoConfiguration的自动装配条件

  • 分析EurekaServerAutoConfiguration
@Configuration(proxyBeanMethods = false)
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({
    EurekaDashboardProperties.class,
		InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer{
   
    ...
}

导入了EurekaServerInitializerConfiguration配置信息

客户端

1.添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--eureka 客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.配置文件application.yml(由于采用多个客户端,客户端采用profile进行区分)

spring:
  application:
    name: discovery-client
eureka:
  client:
    enabled: false
--- # eureka profile
spring:
  profiles: eureka
server:
  port: 8090
eureka:
  client:
    enabled: true
    service-url:
      defaultZone: http://127.0.0.1:9090/eureka/

3.启动类DiscoveryClientApplication

/**
 * 服务发现客户端程序
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/2/1
 */
@SpringBootApplication
//@EnableDiscoveryClient // 这个注解可以去掉
public class DiscoveryClientApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(DiscoveryClientApplication.class, args)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值