Eureka 服务治理 笔记

 

Eureal Serve:  1)服务注册:将服务信息注册进注册中心 2)服务发现:从注册中心获取服务信息

 

已有N个Service Provider:cloud-provider-8001、cloud-provider-8002、...

一个Service Consumer :cloud-consumer-8001

 

1.中首先建立单个EurekaServer

2.单个EurekaServer与Service Provider集群

3、4 EurekaServe集群与Service Provider集群

1 .EurekaServer 服务端

建立一个端口号7001的EurekaServer

1) 创建Module:cloud-eureka-server7001

2)在该Module的pom.xml,引入依赖

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

3)source下添加application.yml

server:
  port: 7001

eureka:
  instance:
    hostname: locathost #eureka服务端的实例名称
  client:
    # false表示不向注册中心注册自己。
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4)主启动 @EnableEurekaServer

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

5)测试,浏览器输入 http://localhost:7001/ ,可进入Spring Eureka服务主页。

2 .微服务入驻EurekaServer

1)在cloud-provider-8001的pom中添加依赖

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

2)application.yml

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

3)主启动类添加@EnableEurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

4)测试:

其他Service Provider 均按照上述修改。

先启动7001对应的Module,再启动8001、8002、....    。浏览器输入 http://localhost:7001   在Eureka主页中lnstances currently registered with Eureka
可看到8001、8002对应的服务。

3. Eureka集群构建

多个eureka server间相互注册

1)按1中方法创建Module  cloud-eureka-server7002、cloud-eureka-server7003、cloud-eureka-server7004、...

2) 映射配置,修改hosts文件

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

3)修改yml文件 中的defaultZone

按照如下方式修改每个EurekaServer的yml文件中的defaultZone,以 cloud-eureka-server7001为例:

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
    #集群指向其它eureka
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/,http://eureka7004.com:7004/eureka/

defaultZone 中指向所有其他的EurekaServer,相互注册,逗号隔开

4. Service Provider 和Service Consumer注册进EurekaServer集群

修改consumer和每个provider的yml文件中的defaultZone

defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka, http://eureka7003.com:7003/eureka, http://eureka7004.com:7004/eureka

列出所有EurekaServer,逗号隔开。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值