spring cloud(一) 服务注册与发现 Eureka||Consul

本系列文章均参考:朝雨忆轻尘,感谢博主!

这里还有他的技术交流群:429854222,欢迎大家支持博主

若有侵权,还请告知,一定删除

Spring Cloud Eureka

server:

pom.xml:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

通过@EnableEurekaServer注解启动一个服务注册中心

@EnableEurekaServer
@SpringBootApplication
public class Application {

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

application.yml:

server:
  port: 8888
eureka:
  server:
    #开启自我保护
    enable-self-preservation: true
  instance:
    #以IP地址注册
    preferIpAddress: true
    hostname: ${spring.cloud.client.ipAddress}
    instanceId: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

client:

pom.xml:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

应用主类中通过加上@EnableEurekaClient注解,启用服务发现(@EnableDiscoveryClient亦可):

@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/

地址对应即可。

Spring Cloud Consul

server:

consul服务器安装详见我其他博客:传送门

client:

pom.xml:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

应用主类中通过加上@EnableDiscoveryClient注解,启用服务发现:

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

application.yml:

spring:
  application:
    name: applicationName
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: ${spring.application.name} # 注册到consul的服务名称

 

集群:

Eureka集群就是开启多个Eureka微服务,之间互相注册即可。然后客户端的配置defaultZone指定一个到多个都行:传送门

consul集群也类似,开启多个consul服务(当然与单机开启命令不一样),然后服务之间通过join命令来开启一个集群:传送门

好,告辞~

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值