Spring Cloud Eureka:服务注册与发现

Eureka简介

     Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

创建Eureka注册中心

这里我们以创建并运行Eureka注册中心来看看在IDEA中创建并运行SpringCloud应用的正确姿势。

1.创建eureka-server模块

2.填写模块信息

3.选择创建的服务

4.添加依赖

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

5.添加@EnableEurekaServer注解来

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

6.配置文件

server:
   port: 8001 #指定运行端口
spring:
   application:
      name: eureka-server #指定服务名称
eureka:
   instance:
      hostname: localhost #指定主机地址
    client:
      fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
      register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
   server:
      enable-self-preservation: false #关闭保护模式

7.启动应用模块

8.运行

9.访问http://localhost:8001/

10.新建client模块

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

11.添加@EnableDiscoveryClient注解

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

12.配置文件

server:
   port: 8101 #运行端口号
spring:
   application:
      name: eureka-client #服务名称
eureka:
   client:
      register-with-eureka: true #注册到Eureka的注册中心
      fetch-registry: true #获取注册实例列表
      service-url:
         defaultZone: http://localhost:8001/eureka/ #配置注册中心地址

13.运行

14.观察注册中心

创建Eureka集群

1.在sever中添加application-replica1.yml

server:
   port: 8002
spring:
   application:
      name: eureka-server
eureka:
   instance:
      hostname: replica1
   client:
      serviceUrl:
         defaultZone: http://replica2:8003/eureka/ #注册到另一个Eureka注册中心
      fetch-registry: true
      register-with-eureka: true

2.在sever中添加application-replica2.yml

server:
   port: 8003
spring:
   application:
      name: eureka-server
eureka:
   instance:
      hostname: replica2
client:
   serviceUrl:
      defaultZone: http://replica1:8002/eureka/ #注册到另一个Eureka注册中心
   fetch-registry: true
   register-with-eureka: true

3.修改本地host文件

127.0.0.1 replica1
127.0.0.1 replica2

4.以application-replica1.yml和application-replica2.yml来启动eureka-server

5.配置

6.访问http://replica1:8002/

7.修改Eureka-client

server:
   port: 8102
spring:
   application:
      name: eureka-client
eureka:
   client:
   register-with-eureka: true
   fetch-registry: true
   service-url:
      defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/ #同时注册到两个注册中心

8.创建一个eureka-security-server模块,添加依赖

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

9.添加application.yml配置文件

server:
   port: 8004
spring:
   application:
      name: eureka-security-server
   security: #配置SpringSecurity登录用户名和密码
      user:
         name: macro
         password: 123456
eureka:
   instance:
      hostname: localhost
   client:
      fetch-registry: false
      register-with-eureka: false

10.添加Java配置WebSecurityConfig

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http.csrf().ignoringAntMatchers("/eureka/**");
      super.configure(http);
   }
}

11.运行eureka-security-server,访问http://localhost:8004发现需要登录认证

12.配置文件中需要修改注册中心地址格式

http://${username}:${password}@${hostname}:${port}/eureka/

13.添加application-security.yml,修改用户名和密码

server:
   port: 8103
spring:
   application:
      name: eureka-client
eureka:
   client:
      register-with-eureka: true
      fetch-registry: true
      service-url:
         defaultZone: http://macro:123456@localhost:8004/eureka/

14.以application-security.yml配置运行eureka-client,可以在注册中心界面看到eureka-client已经成功注册

 

Eureka的常用配置

eureka:
   client: #eureka客户端配置
      register-with-eureka: true #是否将自己注册到eureka服务端上去
      fetch-registry: true #是否获取eureka服务端上注册的服务列表
      service-url:
         defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
      enabled: true # 启用eureka客户端
      registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
   instance: #eureka客户端实例配置
      lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
      lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
      metadata-map:
         zone: jiangsu #所在区域
      hostname: localhost #服务主机名称
      prefer-ip-address: false #是否优先使用ip来作为主机名
   server: #eureka服务端配置
      enable-self-preservation: false #关闭eureka服务端的保护机制

使用到的模块

springcloud-learning
├── eureka-server -- eureka注册中心
├── eureka-security-server -- 带登录认证的eureka注册中心
└── eureka-client -- eureka客户端

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值