Spring Cloud Eureka配置

Spring Cloud Eureka配置

前提:Spring Boot版本:2.2.5.RELEASE

Eureka简介

​ eureka的英文单词的意思是“找到了”,顾名思义,在Spring Cloud中,Eureka是一个“服务发现框架”。

依赖组件:

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

服务端配置

注册中心

​ Eureka服务端,即服务注册中心。它同其他服务注册中心一样,支持高可用配置。依托于强一致性提供良好的服务实例可用性,可以应对多种不同的故障场景。
​ Eureka服务端支持集群模式部署,当集群中有分片发生故障的时候,Eureka会自动转入自我保护模式。它允许在分片发生故障的时候继续提供服务的发现和注册,当故障分配恢复时,集群中的其他分片会把他们的状态再次同步回来。集群中的的不同服务注册中心通过异步模式互相复制各自的状态,这也意味着在给定的时间点每个实例关于所有服务的状态可能存在不一致的现象。

  1. 启动类上面添加注释
    @EnableEurekaServer
    如:

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main (String[] args){
            SpringApplication.run(EurekaServerApplication.class,args);
        }
    }
    
  2. 创建配置文件(application-eureka.yml)

    server:
      port: 8081
    spring:
      application:
        name: eurka-server
      security:
        user:
          name: admin
          password: 123456
    eureka:
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:${server.port}/eureka/
    
  3. 配置权限控制器类(依赖于spring-boot-starter-security),如果已经配置好CSRF,可以忽略这一步。

    @EnableWebSecurity
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http.csrf().disable(); // 关闭csrf
        }
    }
    

客户端配置

​ Eureka客户端,主要处理服务的注册和发现。客户端服务通过注册和参数配置的方式,嵌入在客户端应用程序的代码中。在应用程序启动时,Eureka客户端向服务注册中心注册自身提供的服务,并周期性的发送心跳来更新它的服务租约。同时,他也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期行的刷新服务状态。

服务提供者客户端

  1. 启动类上面添加注释:

    @EnableEurekaClient(和注册中心的注释不一样)

    如:

    @EnableEurekaClient
    @SpringBootApplication
    public class UserProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserProviderApplication.class,args);
        }
    }
    
  2. 创建配置文件(application-eureka.yml)

    server:
      port: 8082
    spring:
      application:
        name: user-provider
      security:
        user:
          name: admin
          password: 123456
    eureka:
      client:
        serviceUrl:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:8081/eureka/
    

服务消费者客户端

  1. 启动类上面添加注释:

    @EnableEurekaClient(同上)

    如:

    @EnableEurekaClient
    @SpringBootApplication
    public class UserProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(UserProviderApplication.class,args);
        }
    }
    
  2. 创建配置文件(application-eureka.yml),注意服务端口号

    server:
      port: 8083
    spring:
      application:
        name: consumer
      security:
        user:
          name: admin
          password: 123456
    eureka:
      client:
        serviceUrl:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:8081/eureka/
    

到这里,一个简易的Eureka框架就已经配置好了。实测通过,如书写有误,欢迎指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值