天天看点

eureka服务端和客户端的简单搭建

     本篇博客简单记录一下,eureka 服务端和 客户端的简单搭建。

目标:

    1、完成单机 eureka server 和 eureka client 的搭建。

    2、完成eureka server 的添加安全认证,即不能别人知道我们的eureka server地址就可以注册上去。

    3、测试环境下,关闭eureka的自我保护

一、eureka server 端的搭建

1、引入依赖

<dependencies>
        <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>

        <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>      

    注意:

           1、由于服务端需要保护,因此还引入了security依赖。

2、编写配置文件

spring:
  application:
    name: eureka-server


server:
  port: 8761
  tomcat:
    uri-encoding: utf-8
eureka:
  client:
    register-with-eureka: false # 由于eureka即可以作为服务端也可以作为客户端,此处是作为服务器段,因此这个参数需要设置成false: 即不作为一个客户端注册到服务注册中心
    fetch-registry: false # true:表示作为一个客户端中eureka server 服务端获取服务注册信息,此处作为一个服务端因此需要设置成 false
    service-url:
      defaultZone : http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
  server:
    # 此处表示关闭 eureka 的自我保护
    enable-self-preservation: false
    # 清理无效节点的时间间隔,默认是60s,此处修改成10s
    eviction-interval-timer-in-ms: 10000

security:
  basic:
    enabled: true # 开启basic认证
  user:
    name: root # 用户名
    password: admin # 密码      

   注意:

          1、默认情况下eureka即可以做为服务端,也可以做为客户端,此处作为服务端,因此需要将 register-with-eureka的值改成false,即不注册到eureka server上。

           2、fetch-registry: 服务端这个值 需要改成 false, 即不去检索服务。

           3、security 开头的配置是因为引入了spring security保护server端,因此 需要注意 service-url 中的 defaultZone 的值的写法 : http://用户名:密码@主机:端口/eureka/

           4、enable-self-preservation的值设置成 false 表示 关闭eureka的自我保护。

                     客户端需要修改下方2个参数的值,正式环境不建议修改。

# 客户端与服务器断心跳的时间间隔,默认为 30秒
            lease-renewal-interval-in-seconds: 3
            # 租约到期时间,此值不可过小,开发环境小点没有事,默认为 90秒
            lease-expiration-duration-in-seconds: 9      

3、编写启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

   注意:

         1、 eureka server 的服务端上需要加上  @EnableEurekaServer 注解,表示作为服务端启动。

 二、eureka client 端的搭建

1、引入依赖

<dependencies>
        <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-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>      

 2、编写配置文件

server:
  port: 8762


eureka:
  client:
    service-url:
      defaultZone : http://${security.user.name}:${security.user.password}@localhost:8761/eureka/   #连接到服务注册中心的地址,如果服务注册中心开启了权限需要设置 http://username:password@ip:port/eureka/格式
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}

    # 客户端与服务器断心跳的时间间隔,默认为 30秒
    lease-renewal-interval-in-seconds: 3
    # 租约到期时间,此值不可过小,开发环境小点没有事,默认为 90秒
    lease-expiration-duration-in-seconds: 9

security:
  user:
    name: root
    password: admin
spring:
  application:
    name: eureka-client


info:
  app:
    name: "eureka-client"
    description: "eureka-client程序"
    version: "0.0.1"      

    注意:

           1、注意一下注册到 eureka server 上 url 的编写格式。

           2、spring.application.name 表示注册到eureka服务上的名字,建议小写。

 3、编写启动类

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

    注意:

           1、eureka client需要加上 @EnableDiscoveryClient 注解,表示这个一个客户端。

三、运行界面

 四、完整代码地址: