Spring Cloud 服务注册发现(1)-Eureka

Spring Cloud 服务注册发现中心 --Eureka

1 单体服务注册发现

1.1 服务注册发现中心

1.1.1 启动代码

 
 
 
       
@SpringBootApplication 
@EnableEurekaServer  //开启EurekaServer 注解
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

1.1.2 Eureka配置文件

application.yml

 
 
 
       
server:
  port: 8761

1.2 服务发布者Provider

1.2.1 Provider配置文件

 
 
 
       
server:
  port: 8000
spring:
  application:
    name: provider
eureka:
  client:
    serviceUrl:
      # 加入Http basic 认证
      defaultZone: http://peer2:8762/eureka/
  instance:
    prefer-ip-address: true

1.3服务消费者Consumer

1.3.1Consumer配置文件

 
 
 
       
server:
  port: 8010
spring:
  application:
    name: consumer
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
  instance:
    prefer-ip-address: true

2 服务注册发现集群

2.1 服务注册发现中心

2.1.1 代码(同上)

2.1.2 Eureka配置文件

pom.xml

 
 
 
       
<!-- 引入spring boot的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  </dependencies>
  <!-- 引入spring cloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Camden.SR4</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

application.yml

 
 
 
       
spring:
  application:
    name: discovery-eureka-ha
---
spring:
  profiles: peer1           # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1          # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/      # 将自己注册到peer2这个Eureka上面去
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

2.3 服务发布者Provider

2.3.1 Provider配置文件

 
 
 
       
server:
  port: 8000
spring:
  application:
    name: provider
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer1:8761/eureka/
  instance:
    prefer-ip-address: true

2.4 服务消费者Consumer

2.4.1Consumer配置文件

 
 
 
       
server:
  port: 8010
spring:
  application:
    name: consumer
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
  instance:
    prefer-ip-address: true

3 Eureka 添加HTTP basic认证

3.1 服务注册发现中心

3.1.1 代码(同上)

3.1.2 Eureka配置文件

pom.xml 引入spring-boot-starter-security包

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

application.yml

 
 
 
       
spring:
  application:
    name: discovery-eureka-ha
security:
  basic:
    enabled: true          #开记 http basic认证
  user:
    name: alex              #设置用户名,默认账户为user
    password: 123456        #设置密码,默认密码随机,启动时会打印
---
spring:
  profiles: peer1           # 指定profile=peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1          # 指定当profile=peer1时,主机名是peer1
  client:
    serviceUrl:
      # 加入Http basic 认证
      defaultZone: http://alex:123456@peer2:8762/eureka/      # 将自己注册到peer2这个Eureka上面去
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      # 加入Http basic 认证
      defaultZone: http://alex:123456@peer1:8761/eureka/

3.2 服务发布者Provider

3.2.1 Provider配置文件

 
 
 
       
server:
  port: 8000
spring:
  application:
    name: provider
eureka:
  client:
    serviceUrl:
      # 加入Http basic 认证
      defaultZone: http://alex:123456@peer2:8762/eureka/
  instance:
    prefer-ip-address: true

3.3 服务消费者Consumer

3.3.1 Consumer配置文件

 
 
 
       
server:
  port: 8010
spring:
  application:
    name: consumer
eureka:
  client:
    serviceUrl:
      # 加入Http basic 认证
      defaultZone: http://alex:123456@peer1:8761/eureka/,http://alex:123456@peer2:8762/eureka/
  instance:
    prefer-ip-address: true

4 Eureka 元数据

分为标准元数据和自定义元数据,很少有场景用到。

自定义

自定义元数据可以使用eureka.instance.metadata-map配置,支持key/value格式

5 Eureka Server Rest端点

Eureka 为非JVM的微服务提供REST端点,可通过RESTful接口来操作Eureka从而实现注册与发现。

6 Eureka 自我保护

6.1 什么是自我保护

采用的是宁可同保留所有微服务,也不盲目注销任何健康的微服务的架构哲学,可以让eureka集群更健壮、稳定。

6.2 自我保护触发出现的红色信息

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

6.3 禁用自我护

eureka.server.enable-self-preservation = false

Eureka 多网卡环境下的IP选择

Spring cloud 提供了按需选择的操作:

操作1:忽略指定名称的网卡

 
 
 
       
#忽略eth0,支持正则表达式
spring:
  cloud:
    inetutils:
      ignored-interfaces:
        --eth0
        --veth.*
eureka:
  instance:
    prefer-ip-address: true

操作2:使用正则表达式,指定使用的网络地址

 
 
 
       
spring:
  cloud:
    inetutils:
      preferredNetworks:
        --192.168
        --10.10
eureka:
  instance:
    prefer-ip-address: true

操作3:只使用站点本地地址

 
 
 
       
spring:
  cloud:
    inetutils:
      use-only-site-local-interfaces:true
eureka:
  instance:
    prefer-ip-address: true

操作4:手动指定IP地址

 
 
 
       
eureka:
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1

猜你喜欢

转载自blog.csdn.net/java_web_jsp0/article/details/80859813