微服务之注册中心Eureka(一)

二、Eureka注册中心 通过案例使用eureka注册中心以及了解eureka的自我保护机制 阅读详情

一、前言

注册中心是微服务架构中基础服务之一,主要是将微服务中的服务地址等信息注册到注册中心,以便使用。
1、eureka中的三大角色
在这里插入图片描述
(1)Service Provider服务提供者 (Server):
服务启动后向注册中心注册自己的一个实例
定期向RegistryCenter发送心跳(TTL), 证明服务正常
服务关闭时向注册中心注销自己(TTL)
(2) Service Consumer服务消费者(Client):
服务启动后RegistryCenter订阅所需要使用的服务(Server), 并缓存到实例列表中。
向对应服务(Server)发起调用时,从内存中的该服务的实例列表选择一个,进行远程调用。
服务关闭时向RegistryCenter取消订阅。
(3) Service Registry Center注册中心:
注册中心一定时间内容未检测到服务提供者的心跳(TTL)时将服务从实例列表移除。
服务的实例列表发生变化(新增或者移除)时,通知订阅该服务的 Consumer,从而让 Consumer 能够刷新本地服务列表缓存。
2、CAP 理论简述
在这里插入图片描述CAP理论是分布式架构中重要理论:
一致性(Consistency) (所有节点在同一时间具有相同的数据)
可用性(Availability) (保证每个请求不管成功或者失败都有响应)
分隔容忍(Partition tolerance) (系统中任意信息的丢失或失败不会影响系统的继续运作)
由于C与A的特性无法共存.CAP 不可能都取,只能取其中2个。
AP:对等部署的多写一直性
CP:基于leader的非对等部署的单点写一致
EUREKA是一种对等部署的多写一致的注册中心
3、注册中心比较
在这里插入图片描述
4、其它
(1)eureka遵循AP原则,无主从节点之分,每一个节点都是对等的,是一种去中心化的注册中心
(zookeeper是一种中心化的注册中心,通过选举的方式决定master节点,当master节点挂了的时候在选举过程中,将会导致服务不可用,需要一些其它的解决办法)
(2)eureka支持多节点部署,因为每个eureka注册中心都是对等的,所以只要集群中还有一个节点存活,服务就可用。
(3)Eureka的自我保护机制:如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,这时Eureka不在从注册表中移除因为长时间没有收到心跳而过期的服务;但此时仍然能够接受新服务的注册和查询请求,但是不会同步到其它节点(保证当前节点可用)。在恢复后在将注册信息同步到其它注册中心中。
(4)上述第三点也是问题所在,出现了上述情况,将无法保证每个注册中心的写一致。

二、开发环境

1、spring-boot 2.2.2
2、spring-cloud Hoxton.SR12
3、jdk 1.8
4、java开发环境
5、idea
6、maven

三、单节点注册中心

1、在idea中新建一个maven project
2、在项目pom中添加公共依赖,项目pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.zlc</groupId>
    <artifactId>micro-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>micro-service</name>
    <description>spring cloud action</description>
    <!-- 父模块公共配置-->
    <dependencyManagement>
        <dependencies>
            <!--spring cloud 依赖-->
            <!--spring boot 与最适spring cloud 版本关系对照可参考如下地址(建议火狐浏览器打开)-->
            <!--https://start.spring.io/actuator/info -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--eureka 注册中心依赖-->
            <dependency>
                <groupId>cn.zlc</groupId>
                <artifactId>eureka-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <!--注册中心服务-->
        <module>eureka-service</module>
    </modules>

    <properties>
        <!--jdk版本-->
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!-- spring boot 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、新建一个注册中心module名为eureka-service,其父pom为项目pom,pom文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.zlc</groupId>
        <artifactId>micro-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-service</name>
    <packaging>jar</packaging>
    <description>eureka 注册中心</description>

    <dependencies>
        <!--eureka 注册中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4、开启注册中心服务(在启动类上添加EnableEurekaServer注解即可)

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

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

}

5、使用yaml文件配置一下eureka启动的端口和应用名称
ps:个人比较爱使用yaml文件,不是properties文件,所以删除了默认的application.properties文件
application.yml文件配置如下:

server:
  # 启动地址
  address: 127.0.0.1
  # 启动端口
  port: 8080
  servlet:
    session:
      # session 超时时间配置,默认30m
      timeout: 600
# spring 配置相关
spring:
  # 应用配置
  application:
    # 应用名称配置
    name: eureka-service

6、可以先启动一下Eureka注册中心,看一下效果

2021-07-26 11:03:19.879  INFO 1048 --- [      Thread-49] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
2021-07-26 11:03:19.880  INFO 1048 --- [      Thread-49] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
2021-07-26 11:03:19.925  INFO 1048 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-26 11:03:19.927  INFO 1048 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8080
2021-07-26 11:03:21.530  INFO 1048 --- [           main] c.z.e.EurekaServiceApplication           : Started EurekaServiceApplication in 23.043 seconds (JVM running for 27.58)
2021-07-26 11:03:23.160  INFO 1048 --- [2)-172.16.1.115] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-26 11:03:23.160  INFO 1048 --- [2)-172.16.1.115] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-07-26 11:03:23.179  INFO 1048 --- [2)-172.16.1.115] o.s.web.servlet.DispatcherServlet        : Completed initialization in 19 ms
2021-07-26 11:03:23.931  INFO 1048 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=java.net.ConnectException: Connection refused: connect stacktrace=com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
	at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187)
	at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123)
	at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27)
	at com.sun.jersey.api.client.Client.handle(Client.java:652)
	at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
	at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
	at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
	at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.register(AbstractJerseyEurekaHttpClient.java:57)

7、访问 http://localhost:8080/在这里插入图片描述
三、注册中心配置
1、在application.yml文件中添加eureka配置

server:
  #address: 127.0.0.1  # 启动地址,不是应用启用不是下面的配置地址则应用无法启动
  port: 8010   # 启动端口
  servlet:
    session:
      timeout: 600 # session 超时时间配置,默认30m

spring:  # spring 配置相关
  application: # 应用配置
    name: eureka-service # 应用名称配置
    #profiles:
    #active:
eureka: # 注册中心Eureka配置
  instance:
    #配置eureka.instance.preferIpAddress = true  可以防止注册中心互相注册注册不上问题  :${random.long(1,100)}
    preferIpAddress: true
    # 唯一标识
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}}
    # 服务续约:在注册完服务后,服务提供者会维持一个心跳用来持续告诉Eureka Server我还活着,以防止Eureka Server的“剔除任务”将服务实例从服务列表中排除出去,我们将该操作称为服务续约(Renew)。
    # 关于服务续约有两个重要的属性,我们可以关注并根据需要来进行调整:
    lease-expiration-duration-in-seconds: 30  # 服务失效的时间,默认90秒。
    lease-renewal-interval-in-seconds: 120 # 服务续约任务的调用间隔时间,默认30秒。
    hostname: 127.0.0.1  #  本地可以是localhost,测试环境以及生产环境需要修改
  server:
    eviction-interval-timer-in-ms: 5000 #(单位毫秒,默认是60*1000)eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
    enable-self-preservation: true   #是否开启保护模式,默认为true ,保护模式就是确保不被无缘无故移除(网络故障但服务正常等情况)
  client:
    register-with-eureka: true  #  是否向注册中心注册自己,默认为true
    fetch-registry: true  # 是否检索服务
    serviceUrl:  # 注册中心地址
      # 单节点配置(没有权限验证)
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2、重新启动eureka(如果启动前报错,因为注册中心还没有给自己注册进去)

2021-07-26 12:30:26.983  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 8ms
2021-07-26 12:30:31.988  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 4ms
2021-07-26 12:30:36.993  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 5ms
2021-07-26 12:30:41.999  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 6ms
2021-07-26 12:30:47.010  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 10ms
2021-07-26 12:30:52.011  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 1ms
2021-07-26 12:30:57.028  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 16ms
2021-07-26 12:31:02.038  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 10ms
2021-07-26 12:31:07.040  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 1ms
2021-07-26 12:31:12.048  INFO 7656 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 7ms

3、访问 eureka: http://127.0.0.1:8010/
在这里插入图片描述
4、Eureka面板与项目启动端口一致

四、eureka客户端

1、eureka客户端取名叫做eureka-clinet,在pom文件中添加一个web依赖,可以让程序正常启动以后不关闭,pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.zlc</groupId>
        <artifactId>micro-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <packaging>jar</packaging>
    <description>Eureka-client 注册中心客户端</description>

    <dependencies>
        <!-- eureka客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、开启eureka客户端注解

/**
 * @author  jacob
 * EnableEurekaClient 开启eureka 客户端连接
 */
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

3、配置eureka客户端

server:
  port: 8020

spring:
  application:
    name: eureka-client-service
  #profiles:
    #active: dev,eureka-client1
logging:
  level:
    org:
      springframework:
        cloud:
          loadbalancer:
            config:
              BlockingLoadBalancerClientAutoConfiguration: error

# 注册中心配置  :${random.long(1,100)
eureka:
  instance:
    preferIpAddress: true
    #instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    instance-id: ${spring.application.name}:${server.port}
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
  client:
    register-with-eureka: true
    fetch-registry: false
    serviceUrl:
      defaultZone: http://127.0.0.1:8010/eureka/

4、先启动eureka-service,然后在启动eureka-client,正常启动成功后访问注册中心
http://127.0.0.1:8010/
在这里插入图片描述
两个服务全部注册到注册中心了

五、Eureka启动中的警告处理

1、

WARN 4528 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.

这个已经有issue了,可以暂时忽略
2、

WARN 4528 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.

解决办法:
(1)在项目中添加一个空的config.properties文件
(2)通过启动参数“-Darchaius.configurationSource.additionalUrls”制定配置文件

-Darchaius.configurationSource.additionalUrls="classpath:extra.properties,file:///home/user/other-extra.properties"

(3)如果不使用archaius组件前提下,排除netflix-archaius引入。如还引入了其他netflix组件,可能也会有archaius引入,需要一起exclusion。

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <exclusions>
                <!--fix No URLs will be polled as dynamic configuration sources. warring-->
                <!-- netflix-archaius包,默认需要加载配置源,无法加载配置元导致系统警告。-->
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-archaius</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3、

 WARN 7036 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.

经上述源码分析我们应该很清楚为什么会打印这么一条warn警告日志了,这是官方想推荐自己的负载均衡客户端BlockingLoadBalancerClient,但是BlockingLoadBalancerClient还有很多不完善的地方,还是老老实实的用Ribbon吧;警告对我们使用ribbon没有任何影响,如果看着不舒服的话可以关掉,通过配置当前类日志级别的方式可以做到:

4、如果排除了上面的

  <exclusion>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </exclusion>

则会产生一个新的警告:

Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.

解决办法:
(1)

spring.cloud.loadbalancer.cache.enabled=false

(2)添加依赖

<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
        <version>2.8.8</version>
    </dependency>

五、增强eureka安全
上面的操作eureka客户端通过http连接了eureka注册中心,不能够保证安全性,我们可以通过一些简单的配置保护我们的注册中心
1、为eureka添加security保证安全

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

2、修改配置文件,添加security用户名和密码用户权限访问

server:
  address: 127.0.0.1  # 启动地址,不是应用启用不是下面的配置地址则应用无法启动
  port: 8010   # 启动端口
  servlet:
    session:
      timeout: 600 # session 超时时间配置,默认30m

spring:  # spring 配置相关
  application: # 应用配置
    name: eureka-service # 应用名称配置
  security:
    user:
      name: yourusername
      password: pw123456
    #profiles:
    #active:
# 日志配置
logging:
  level:
    org:
      springframework:
        cloud:
          loadbalancer:
            config:
              BlockingLoadBalancerClientAutoConfiguration: error
eureka: # 注册中心Eureka配置
  instance:
    #配置eureka.instance.preferIpAddress = true  可以防止注册中心互相注册注册不上问题  :${random.long(1,100)}
    preferIpAddress: true
    # 唯一标识(可以通过服务名+端口或者ip+端口的方式指定)
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
    # 服务续约:在注册完服务后,服务提供者会维持一个心跳用来持续告诉Eureka Server我还活着,以防止Eureka Server的“剔除任务”将服务实例从服务列表中排除出去,我们将该操作称为服务续约(Renew)。
    # 关于服务续约有两个重要的属性,我们可以关注并根据需要来进行调整:
    lease-expiration-duration-in-seconds: 30 # 服务续约任务的调用间隔时间,默认30秒。
    lease-renewal-interval-in-seconds: 90  # 服务失效的时间,默认90秒。
    hostname: 127.0.0.1  #  本地可以是localhost,测试环境以及生产环境需要修改
  server:
    eviction-interval-timer-in-ms: 120000 #(单位毫秒,默认是60*1000)eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
    enable-self-preservation: true   #是否开启保护模式,默认为true ,保护模式就是确保不被无缘无故移除(网络故障但服务正常等情况)
  client:
    register-with-eureka: true  #  是否向注册中心注册自己,默认为true
    fetch-registry: true  # 是否检索服务
    serviceUrl:  # 注册中心地址
      # 单节点配置(没有权限验证)
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

3、添加一个websecurity配置类

package cn.zlc.eurekaservice.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 当配置了security后,如有没有用户名和密码,则服务无法注册到注册中心,
 * 目前在配置文件中配置的方法已经过时了
 * @author : jacob
 **/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        // 关闭 csrf
        http.csrf().disable();
    }
}

4、启动eureka-service,访问http://127.0.0.1:8010/ 需要我们输入用户名和密码成功登录以后才能查看eureka信息。
在这里插入图片描述
5、输入在配置文件中配置的用户名和密码,成功登录后我们可以看到注册中心有eureka-service
在这里插入图片描述
6、eureka-client 客户端连接注册中心,修改配置如下:

server:
  port: 8020

spring:
  application:
    name: eureka-client-service
  security:
    user:
      name: yourusername
      password: pw123456
    #profiles:
    #active: dev,eureka-client1
logging:
  level:
    org:
      springframework:
        cloud:
          loadbalancer:
            config:
              BlockingLoadBalancerClientAutoConfiguration: error

# 注册中心配置  :${random.long(1,100)
eureka:
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name}
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    hostname: 127.0.0.1
  client:
    register-with-eureka: true
    fetch-registry: false
    serviceUrl:
      #defaultZone: http://127.0.0.1:8010/eureka/  # 单节点无权限
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@127.0.0.1:8010/eureka/ #单节点有权限

7、程序正常启动成功,访问http://127.0.0.1:8010/ 可以看到两个服务都在注册中心中
在这里插入图片描述
六、声明
本文首发于香菜喵,打开微信随时随地读,获取更多资源文章下方 ↓ ↓ ↓

Spring Cloud 微服务实战系列-Eureka注册中心(一) 导语   在这一个系列的分享中,笔者主要是来分享在实战实际操作中的一些使用场景,了解了实战的操作中的使用场景才能更好的从实战中的问题出发找到自己需要的原理性的东西,找到原理性的东西之后才可以更好的根据原理性的东西进行内容上的扩展不断的提升自己的能力,实现从零到一的突破。 文章目录Eureka介绍使用Eureka搭建服务注册中心创建一个Maven项目创建启动类配置文件编写服务提供者创建Maven项目编写启动类编写配置文件启动服务提供者编写服务消费者直接调用上面接口启动类总结   注册中心在微服务架构中是不. 阅读详情

相关推荐

spring cloud 微服务实战 统一注册中心eureka(一)

前言 统一注册中心作为微服务架构下的前提必要条件,发挥着越来越重要的作用,今天小编带大家了解以下springCloud下的统一注册中心 架构图 基本知识 1 . springCloud eureka 是基于Netfilx Eureka的第二次封装 2 . 主要有两个组件组成 一个是Eureka client 另外一个是Eureka server 3. 主要作为作为轮循服务的负载均衡器 ...

小伍哥的博客 1127

什么是Eureka?Eureka能干什么?Eureka怎么用?

Spring Cloud 封装了 公司开发的 Eureka 模块来实现服务治理服务治理就是提供了微服务架构中各微服务实例的快速上线或下线且保持各服务能正常通信的能力的方案总称。服务治理的优点: 是 开源微服务框架中一系列项目中的一个。对其进行了二次封装,形成了 子项目,但未对微服务实现原理进行更改,只是进行了化,使开发者更容易使用和整合。在中,对于服务治理有如下3个概念:Eureka Server提供注册服务功能 EurekaClient通过注册中心进行访问 1.4、 什么场景使用Eureka 准确来说是

怪咖@的博客 2万+

Spring Cloud - Eureka原理、注册、搭建、应用(全过程详解)

上一章我们讲到,利用 RestTemplate 发送 http 请求时,将 url 写成了 "http://localhost:8081/user/" + order.getUserId() ,这无疑将 url 写死了,?这只是其中一个背景,总的来讲,有以下三种原因:服务消费者该如何获取服务提供者的?如果有多个服务提供者,消费者该?消费者如何得知服务提供者的?

CYK_byte的博客 1万+

注册中心【Eureka】

通常微服务自身的故障关闭,只会导致个别微服务出现故障,一般不会大面积出现故障,而网络故障会导致eureka server在短时间内无法收到大批心跳,考虑到这个区别,eurake设定了一个阈值,当判断挂掉的服务的数量超过阈值时,eureka server认为很大程度上出现了网络故障,将不再删除心跳过期服务。自我保护的条件:一般情况下,微服务在eureka上注册后,会30s定期发送心跳,eureka通过心跳来判断微服务是否健康,同时会定期删除超过90s没有发送心跳的服务。2、Renew(服务续约):发送心跳;

weixin_44441657的博客 2729

SpringCloud之Eureka:服务注册与发现全面教程!

在分布式系统中,服务治理是一个至关重要的环节。Eureka,作为Spring Cloud微服务架构中的服务注册与发现组件,扮演着至关重要的角色。下面,我们将详细探讨Eureka的作用及其在微服务架构中的重要性。通过上述配置,你可以为Eureka服务创建一个高可用的集群环境。这将确保你的微服务架构能够抵御单点故障,并提供持续的服务注册与发现能力。记住,高可用性不仅仅是配置多个服务实例,还包括考虑网络分区、脑裂问题、负载均衡、监控和安全等因素。正确配置Eureka的高可用性是构建稳定、可靠的分布式系统的关键。

邓邓子的博客 3148

从IR Drop到EM分析:数字IC电源规划中那些没人明说的细节(以MTCMOS配置为例)

本文深入探讨数字IC后端设计中电源规划的关键细节,特别关注MTCMOS配置、电源网络电阻最小化和level shifter供电等实战技巧。通过分析IR Drop和EM问题,提供优化floorplan和powerplan的具体策略,帮助工程师避免常见陷阱,提升芯片设计的稳定性和可靠性。

q9w8e7r6t5的博客 511

《SpringCloud微服务 一》之 Eureka 服务注册中心

《SpringCloud微服务 一》之 Eureka 服务注册中心 目录《SpringCloud微服务 一》之 Eureka 服务注册中心一、Eureka 服务注册中心1.1 eureka 服务端1.2 eureka 客户端 一、Eureka 服务注册中心 工程 spring-cloud-learn-eureka eureka 注册中心 1.1 eureka 服务端 第一步:引入 eureka 服务端依赖: pom.xml <dependency> <groupId>o

天涯共明月的博客 575

Adams与Matlab联合仿真入门:从零搭建你的第一个动力学模型

本文为Adams与Matlab联合仿真新手提供了一份详尽的入门指南。文章从软件环境配置、版本兼容性检查讲起,逐步演示了如何在Adams中构建单摆动力学模型并定义输入输出接口,以及在Matlab/Simulink中搭建控制回路。通过一个完整的受控单摆实例,手把手教你完成从模型导出、模块生成到执行联合仿真的全流程,帮助你快速搭建并运行第一个联合仿真模型,实现动力学与控制系统的协同验证。

g5h6i7j的博客 699

SpringCloud微服务之注册中心Eureka(一)

一、新建一个母项目作为版本控制 1.新建一个maven项目,引入pom如下,作为控制版本使用的母项目 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.

Acfunwei 213

安卓风控对抗实战:如何用AOSP定制ROM绕过Momo、AIDA64等检测(从环境搭建到设备画像)

本文详细解析了如何通过定制AOSP ROM绕过Momo、AIDA64等安卓风控检测工具的设备指纹识别。从环境搭建到硬件参数修改,再到内核级特征混淆,提供了一套完整的底层对抗方案,显著提升设备伪装通过率。

weixin_30687051的博客 490

头歌OpenGauss数据库 - 单表查询 答案

本关任务:用SELECT语句检索数据表中指定字段的数据;用SELECT语句检索数据表中所有字段的数据。为了完成本关任务,你需要掌握: 1.如何获取数据表中指定字段的数据;2.如何获取数据表中的所有数据。

d214493517的博客 5720

微服务学习入口 - (一)Eureka注册中心

Eureka注册中心在微服务架构中是必不可少的一部分,主要用来实现服务治理功能,总之,很重要。 如果需要看懂后面的文档,需要有一定的Spring Boot和maven的基础。 使用Eureka编写注册中心服务 这里使用Maven多模块来管理项目,maven多模块这里就不多说了。Spring cloud版本为:2.2.0.RELEASE,Spring boot版本为:2.2.1.RELEASE,Spring版本为:5.2.1.RELEASE。 父模块:demo、三个子模块:demo-eureka(注册中心)、

admin_15082037343的博客 778

电机控制——PMSM无感FOC控制(一)FOC入门

FOC 是一种电机控制技术,全称为 Field Oriented Control(磁场定向控制),也称作矢量控制。该技术可以提高电机的效率、控制稳定性和精度,广泛应用于电机驱动系统中,是目前无刷直流电机(BLDC)、永磁同步电机(PMSM)、感应电机的高效控制的最优方法之一。本节主要讲了FOC的大致流程,带大家先了解FOC到底是个什么东西在做什么,各个模块具体的原理会放到后面的章节详细讲解。FOC其实就是通过控制,相电流按照正弦变化,从而产生出旋转的磁场,控制电机转子转动。

zhuxinmingde的博客 2624

SpringCloud 之 Eureka 服务注册中心 [ 注册数据、视图微服务 ](一)

一、创建父工程 LinZeCloud。 父工程已经创建过了,不懂的 前往上一篇 ,也可直接下载上一篇项目文件:下载 206KB,下载后就不用搭了。 然后删除其他的文件,如下图就OK 。 二、创建子工程 linze-publics 公共文件。 注:用于存放公共文件。 2.1 工程创建。 2.2 创建目录与文件。 2.2.1 创建 src/main/java/cn/lz/cloud/publi...

Hack_Different的博客 1408

WIFI CSI动作识别数据集全解析:从数据采集到深度学习应用

本文全面解析了利用商用Wi-Fi设备的信道状态信息进行动作识别的完整流程。文章以经典WiSA数据集为例,深入剖析了CSI数据的采集原理、结构特点,并详细阐述了从原始数据预处理、特征工程到深度学习模型(如CNN-LSTM融合架构)构建与优化的关键技术,旨在为研究者提供从数据到应用的全链路实战指南。

r2s3t4的博客 281

基于STM32L4XX、HAL库的BQ25713RSNR充电芯片驱动程序设计

define BQ25713_I2C_ADDR (0x6B << 1) // 7位地址为 0x6B,HAL库需要左移一位。// 示例:配置 CHARGE_OPTION_0 寄存器:使能充电,使能 ICO(输入电流优化)reg_data |= (EN_CHG_MASK | EN_ICO_MASK);

zhusl6688的博客 406

SpringCloud搭建微服务框架(一)- eureka注册中心

开始搭建框架之前,先简要介绍下spring cloud的微服务框架: eureka服务发现与注册框架:服务注册与发现,可以细分为eureka server(服务注册中心)和eureka client(服务注册客户端,所有其他需注册到服务注册中心的微服务组件都可以看做是服务注册客户端) Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样Eu...

IT的鱼 397

聊聊蓝牙配对技术-CTKD OVER BR/EDR

摘要:本文探讨了蓝牙设备通过跨传输密钥派生(CTKD)机制实现双模连接的技术细节。研究发现,手机在连接双模耳机时优先通过BR/EDR配对,再利用CTKD派生BLE连接所需的密钥。关键条件包括:设备地址一致性、启用安全连接(Secure Connection)及禁用justworks配对方式。文章详细剖析了从BR/EDR链路密钥派生BLE长期密钥(LTK)的算法流程,重点解析了H6/H7加密函数的作用机制与安全差异。研究还通过HCI日志验证了密钥交换和加密建立的完整过程,指出蓝牙5.1版本对CTKD安全性的强

十多年蓝牙协议软件开发,擅长蓝牙协议分析 2692

SpringCloud微服务实战:一、Eureka注册中心服务端

1.项目启动类application.java类名上增加@EnableEurekaServer注解,声明是注册中心 1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 imp...

banqi3433的博客 169

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

SpringCloud构建微服务(一)Eureka 服务注册中心

微服务架构-SpringCloud前言Spring Cloud简介微服务架构服务注册和发现注册中心启动项目文件配置文件注册中心启动服务提供方启动文件配置文件启动服务客户端 前言 发表Spring Cloud系列的文章都是我个人在使用过程中的记录,所以,有可能会存在一些错误或不当的地方,欢迎指正和意见交流。纯属于个人对技术的探索与喜爱,非常高兴与大家一起分享。 Spring Cloud简介 Spri...

卡哇伊的码农 350

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【SpringCloud-微服务系列(一)Eureka注册中心详解】

Netflix公司提供的服务注册和发现组件Netflix相关的组件:Eureka、Ribbon、Hystrix、Zuul、Config等注册中心的作用:服务提供者启动后将自己的IP和端口注册到注册中心上,服务消费者从注册中心获得服务提供者的IP和端口进行连接,服务提供者和消费者每隔30s向注册中心发送心跳,如果90s没有发送,注册中心会剔除掉失效的服务器。...............

清风ikl的博客 377

sprinboot系列七——微服务一——eureka注册中心集群搭建

集群组件以及关系 部署(eureka-client *2 ,ribbon 1 ,eureka-server3) eureka-client eureka-client:微服务的服务具体实现,单节点具体服务注册,注册到同一eureka集群可实现集群部署 ribbon:负载均衡,客户端访问的是该节点,实现多种负载均衡策略 eureka-server EurekaServer同时也是EurekaClie...

qq_31443653的博客 415

Eureka注册中心:实现微服务架构下的服务发现与治理的艺术(一)

Eureka注册中心是Netflix开发的一个基于REST的服务,主要用于微服务架构中的服务发现与治理。在分布式系统中,Eureka注册中心充当了一个“通讯录”的角色,记录了服务和服务地址的映射关系。服务提供者会将自己的信息注册到Eureka Server中,而服务消费者则通过Eureka Server来查找和调用所需的服务。

凛鼕将至的博客 1554

微服务从零到一 构建高可用的注册中心(Eureka版)

构建高可用的注册中心(Eureka版) 一、搭建高可用的Eureka的作用   当服务器因种种原因导致Eureka注册中心(后面简称Eureka)服务当机(服务器跪了,异常关闭停止服务)。这样就会影响到整个业务的流程,因为你把所有的业务都注册到了Eureka中,当Eureka所依赖的docke...

honger_hua的博客 323
上一篇: Failed to process import candidates for configuration class [cn.zlc.eurekaservice.EurekaServiceAppl
下一篇: 微服务之注册中心Eureka(二)
喵喵@香菜
博客等级 码龄10年 1万+粉丝 126原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵喵@香菜

感谢观众老爷送的一发火箭!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值