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

小鱼儿 2024-02-19 18:26 130阅读 0赞

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

目标:

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

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

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

一、eureka server 端的搭建

1、引入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>
  16. <build>
  17. <plugins>
  18. <plugin>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-maven-plugin</artifactId>
  21. </plugin>
  22. </plugins>
  23. </build>

注意:

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

2、编写配置文件

  1. spring:
  2. application:
  3. name: eureka-server
  4. server:
  5. port: 8761
  6. tomcat:
  7. uri-encoding: utf-8
  8. eureka:
  9. client:
  10. register-with-eureka: false # 由于eureka即可以作为服务端也可以作为客户端,此处是作为服务器段,因此这个参数需要设置成false: 即不作为一个客户端注册到服务注册中心
  11. fetch-registry: false # true:表示作为一个客户端中eureka server 服务端获取服务注册信息,此处作为一个服务端因此需要设置成 false
  12. service-url:
  13. defaultZone : http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
  14. instance:
  15. hostname: localhost
  16. server:
  17. # 此处表示关闭 eureka 的自我保护
  18. enable-self-preservation: false
  19. # 清理无效节点的时间间隔,默认是60s,此处修改成10s
  20. eviction-interval-timer-in-ms: 10000
  21. security:
  22. basic:
  23. enabled: true # 开启basic认证
  24. user:
  25. name: root # 用户名
  26. 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个参数的值,正式环境不建议修改。

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

3、编写启动类

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaServerApplication.class, args);
  6. }
  7. }

注意:

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

二、eureka client 端的搭建

1、引入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-actuator</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>

2、编写配置文件

  1. server:
  2. port: 8762
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone : http://${security.user.name}:${security.user.password}@localhost:8761/eureka/ #连接到服务注册中心的地址,如果服务注册中心开启了权限需要设置 http://username:password@ip:port/eureka/格式
  7. instance:
  8. prefer-ip-address: true
  9. instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
  10. # 客户端与服务器断心跳的时间间隔,默认为 30秒
  11. lease-renewal-interval-in-seconds: 3
  12. # 租约到期时间,此值不可过小,开发环境小点没有事,默认为 90秒
  13. lease-expiration-duration-in-seconds: 9
  14. security:
  15. user:
  16. name: root
  17. password: admin
  18. spring:
  19. application:
  20. name: eureka-client
  21. info:
  22. app:
  23. name: "eureka-client"
  24. description: "eureka-client程序"
  25. version: "0.0.1"

注意:

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

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

3、编写启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class EurekaClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaClientApplication.class, args);
  6. }
  7. }

注意:

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

三、运行界面

1、浏览器上输入 : http://localhost:8761 回车后,填写用户名(root)/密码(admin) 登录即可看到这个页面
aHR0cDovL2RsMi5pdGV5ZS5jb20vdXBsb2FkL2F0dGFjaG1lbnQvMDEyOS85MDkzLzEyMWJhMmI2LTEzYTgtM2FkYi1iMGU1LWQ0YjcxMzNjMjcyMy5wbmc

四、完整代码地址:

https://gitee.com/huan1993/spring-cloud-parent.git 里面 eureka-server和eureka-client即为本篇博客的代码。

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读