spring cloud篇-(高可用注册中心Eureka集群搭建)

野性酷女 2022-11-18 01:28 310阅读 0赞

SpringCloud开发高可用注册中心Eureka集群

  • 项目初始化构建
  • 初始化EurekaServer
  • 开发product和consumer端
    • product实现
    • Consumer端
  • 为EurekaServer添加认证功能
  • 配置EurekaServer高可用集群

项目初始化构建

项目使用Idea开发,下面使用Spring Initializr初始化spring cloud项目
1.选择Sprign Initializr,点击右下角的Next
在这里插入图片描述
2.修改项目构建信息,点击Next
在这里插入图片描述
3.选择Spring Cloud Discovery下面的Eureka Server,然后点击Next
注意,这里使用的是Spring Boot 2.4.4版本

在这里插入图片描述

4.选择项目名称,目录,然后点击Finish
在这里插入图片描述

初始化EurekaServer

初始化完成之后,项目结构如下
在这里插入图片描述
1.在resources目录下面创建application.yaml文件,并在里面写入eureka server的配置信息

  1. server:
  2. port: 8761
  3. eureka:
  4. server:
  5. eviction-interval-timer-in-ms: 60000 #驱逐下线服务的间隔时间
  6. enable-self-preservation: ${ EUREKA_ENABLE_SELF_PRESERVATION:false} #关闭eureka服务自我保护机制,使eviction-interval-timer-in-ms配置生效
  7. instance:
  8. health-check-url-path: /actuator/health #健康检查地址
  9. prefer-ip-address: true #显示ip地址
  10. hostname: ${ EUREKA_HOSTNAME:localhost}
  11. client:
  12. fetch-registry: false #注册中心不需要拉取服务
  13. register-with-eureka: false #使用高可用集群搭建,设置为false即可
  14. service-url:
  15. defaultZone: ${ EUREKA_SERVER_LIST:http://localhost:8761/eureka} #注册中心地址
  16. spring:
  17. application:
  18. name: eureka-server

2.在EurekaServerApplication类上加上@EnableEurekaServer注解,标识这是Eureka注册中心服务端
在这里插入图片描述
3.启动项目
在这里插入图片描述
4.访问http://localhost:8761查看eureka dashboard
在这里插入图片描述

开发product和consumer端

product实现

1.项目依赖
在这里插入图片描述
2.resources目录创建application.yaml,并添加如下信息到application.yml

  1. spring:
  2. application:
  3. name: Product
  4. eureka:
  5. instance:
  6. prefer-ip-address: true
  7. health-check-url-path: /actuator/health
  8. client:
  9. register-with-eureka: true
  10. fetch-registry: false #生产端不需要拉取服务
  11. service-url:
  12. defaultZone: http://localhost:8761/eureka #注册中心地址
  13. server:
  14. port: 9090

3.查看eureka-server的web界面,生产者已经注册到eureka上了
在这里插入图片描述
4.编写controler,为consumer提供服务
在这里插入图片描述

  1. package com.lhstack.product.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /** * @author lhstack */
  6. @RestController
  7. @RequestMapping
  8. public class TestController {
  9. @GetMapping("/hello")
  10. public String hello(){
  11. return "Hello World";
  12. }
  13. }

Consumer端

1.项目依赖
在这里插入图片描述
2.application.yml

  1. spring:
  2. application:
  3. name: Consumer
  4. eureka:
  5. instance:
  6. prefer-ip-address: true
  7. health-check-url-path: /actuator/health
  8. client:
  9. register-with-eureka: true
  10. fetch-registry: true #消费端需要发现服务
  11. service-url:
  12. defaultZone: http://localhost:8761/eureka #注册中心地址
  13. server:
  14. port: 8080

3.在启动类加上@EnableDiscoveryClient,标识需要发现服务
在这里插入图片描述
4.编写controller,并调用Product暴露的服务
在这里插入图片描述

  1. package com.lhstack.consumer.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.web.client.RestTemplateBuilder;
  4. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  5. import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.client.RestTemplate;
  12. /** * @author lhstack */
  13. @RestController
  14. @RequestMapping
  15. public class ConsumerController {
  16. @Autowired
  17. private RestTemplate restTemplate;
  18. /** * LoadBalanced 注解会在RestTemplate中添加一个Interceptor,用于解析url地址,获取服务名,然后调注册中心的服务列表接口进行负载均衡 * @return */
  19. @Bean
  20. @LoadBalanced
  21. public RestTemplate restTemplate(){
  22. return new RestTemplate();
  23. }
  24. @GetMapping
  25. public String consumer(){
  26. return restTemplate.getForObject("http://Product/hello",String.class);
  27. }
  28. }

5.启动服务,访问接口
在这里插入图片描述

为EurekaServer添加认证功能

1.eureka-server项目的pom.xml添加spring-security依赖
在这里插入图片描述

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

2.编写Security的配置类
在这里插入图片描述

  1. package com.lhstack.registration.eureka.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. /** * @author lhstack */
  8. @EnableWebSecurity
  9. public class EurekaSecurityConfiguration extends WebSecurityConfigurerAdapter {
  10. @Value("${eureka.auth.enable:false}")
  11. private Boolean enableAuth;
  12. @Value("${eureka.auth.username:admin}")
  13. private String username;
  14. @Value("${eureka.auth.password:123456}")
  15. private String password;
  16. @Override
  17. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  18. auth.inMemoryAuthentication().withUser(this.username)
  19. .password("{noop}" + password)
  20. .authorities("ADMIN");
  21. }
  22. @Override
  23. protected void configure(HttpSecurity http) throws Exception {
  24. if(enableAuth){
  25. http.authorizeRequests()
  26. .antMatchers("/actuator/**")
  27. .permitAll()
  28. .anyRequest()
  29. .authenticated()
  30. .and()
  31. .csrf()
  32. .disable()
  33. .formLogin()
  34. .disable()
  35. .logout()
  36. .disable()
  37. .httpBasic(); //开启basic认证
  38. }else {
  39. http.authorizeRequests()
  40. .anyRequest()
  41. .permitAll();
  42. }
  43. }
  44. }

3.在application.yaml文件中开启eureka-server的授权认证

  1. server:
  2. port: 8761
  3. eureka:
  4. auth:
  5. enable: true #开启认证
  6. username: root #设置认证用户名
  7. password: 123456 #设置认证密码
  8. server:
  9. eviction-interval-timer-in-ms: 60000 #驱逐下线服务的间隔时间
  10. enable-self-preservation: ${ EUREKA_ENABLE_SELF_PRESERVATION:false} #关闭eureka服务自我保护机制,使eviction-interval-timer-in-ms配置生效
  11. instance:
  12. health-check-url-path: /actuator/health #健康检查地址
  13. prefer-ip-address: true #显示ip地址
  14. hostname: ${ EUREKA_HOSTNAME:localhost}
  15. client:
  16. fetch-registry: false #注册中心不需要拉取服务
  17. register-with-eureka: false #使用高可用集群搭建,设置为false即可
  18. service-url:
  19. defaultZone: ${ EUREKA_SERVER_LIST:http://localhost:8761/eureka} #注册中心地址
  20. spring:
  21. application:
  22. name: eureka-server

4.启动Eureka-Server并访问其web页面
然后需要我们输入用户名和密码,这里输入配置文件里面些的root 123456
在这里插入图片描述
登录成功之后,成功进入页面
在这里插入图片描述
5.修改之前的product和consumer项目的application.yaml,添加basic认证
将原本的defaultZone后面的url修改为http://root:123456@localhost:8761/eureka
然后分别启动product和consumer项目
服务成功注册到eureka-server
在这里插入图片描述
6.请求consumer的接口,查看是否能调用product暴露的服务
在这里插入图片描述

配置EurekaServer高可用集群

1.修改eureka-server项目下的application.yaml

  1. server:
  2. port: 8761 #这里对应也要设置成对应端口 8761,8762,8763
  3. eureka:
  4. auth:
  5. enable: true
  6. username: root
  7. password: 123456
  8. server:
  9. eviction-interval-timer-in-ms: 60000 #驱逐下线服务的间隔时间
  10. enable-self-preservation: ${ EUREKA_ENABLE_SELF_PRESERVATION:false} #关闭eureka服务自我保护机制,使eviction-interval-timer-in-ms配置生效
  11. instance:
  12. health-check-url-path: /actuator/health #健康检查地址
  13. prefer-ip-address: true #显示ip地址
  14. hostname: ${ EUREKA_HOSTNAME:localhost}
  15. client:
  16. fetch-registry: false #注册中心不需要拉取服务
  17. register-with-eureka: false #使用高可用集群搭建,设置为false即可
  18. service-url:
  19. defaultZone: ${ EUREKA_SERVER_LIST:http://root:123456@localhost:8761/eureka,http://root:123456@localhost:8762/eureka,http://root:123456@localhost:8763/eureka} #这里设置三个地址,分别启动三台eureka-server
  20. spring:
  21. application:
  22. name: eureka-server

这里使用的是Idea的多开模式,2020.3的设置如下
在这里插入图片描述
在这里插入图片描述
第一台
在这里插入图片描述

第二台
在这里插入图片描述

第三台
在这里插入图片描述

2.分别访问对应页面
第一台
在这里插入图片描述

第二台
在这里插入图片描述
第三台
在这里插入图片描述
可以看到,三台上面都注册了服务,是之前第一台上面注册的,这种高可用方式,会将服务同步到每一台集群,所以只要服务注册到其中一台,其他服务就能通过集群中另一台注册中心获取到这个服务

发表评论

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

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

相关阅读