专注于 JetBrains IDEA 全家桶,永久激活,教程
提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,等工具的永久激活

springboot项目中使用Eureka作服务注册发现

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

Eureka组件
Eureka包含两个组件:Eureka Server和Eureka Client。

Eureka Server
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Server本身也是一个服务,默认情况下会自动注册到Eureka注册中心。
如果搭建单机版的Eureka Server注册中心,则需要配置取消Eureka Server的自动注册逻辑。毕竟当前服务注册到当前服务代表的注册中心中是一个说不通的逻辑。
Eureka Server通过Register、Get、Renew等接口提供服务的注册、发现和心跳检测等服务。

Eureka Client
Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。
在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
Eureka Client分为两个角色,分别是:Application Service(Service Provider)和Application Client(Service Consumer)

搭建Eureka Server
创建springcloud项目springcloud-eureka-register-center

加入netflix-eureka jar包

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

添加配置application.properties

eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true

  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 6000

  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类加@EnableEurekaServer

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

搭建Eureka Client
1)搭建Service Provider
创建springcloud项目springcloud-eureka-service-provider

加入netflix-eureka jar包

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

添加配置application.properties

eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
eureka.instance.hostname=${spring.application.name}
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true

启动类加@EnableEurekaClient

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

2)搭建Service Consumer
创建springcloud项目springcloud-eureka-service-consumer

加入netflix-eureka jar包

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

添加配置application.properties

eureka.client.service-url.defaultZone=http://localhost:9999/eureka/
eureka.instance.hostname=service-consumer-01
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true

启动类加@EnableEurekaClient

@SpringCloudApplication
@EnableEurekaClient
public class ServiceConsumerApplication {

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

测试
先启动springcloud-eureka-register-center,浏览器中访问 http://localhost:9999
再启动springcloud-eureka-service-provider,
最后启动springcloud-eureka-service-consumer,
然后刷新http://localhost:9999页面,页面中可见到springcloud-eureka-service-provider,springcloud-eureka-service-consumer的服务名。

73_1.png

源码示例:https://gitee.com/lion123/springcloud-eureka-demo

-——————————————————-

eureka注册中心访问权限

<!-- 添加注册中心权限依赖  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

在注册中心服务添加bootstrop.yml文件,内容如下

security:
    basic:
      enabled: true
    user:
      name: admin
      password: admin123

客户端服务application.properties文件中配置注册中心地址中加入访问的用户名和密码

eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9999/eureka/

-——————————————————-

哪些情况注册中心会找不到注册的服务?
1)注册中心是否可看到要调用的服务,如果没有看注册地址有没有对,看有没有添加@EnableEurekaClient注解。
2)若注册中心可看到要调用的服务,看服务层代码是否添加了@Service注解。

文章永久链接:https://tech.souyunku.com/24904

未经允许不得转载:搜云库技术团队 » springboot项目中使用Eureka作服务注册发现

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们