服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringCloud搭建netflix-eureka微服务集群的过程详解

SpringCloud搭建netflix-eureka微服务集群的过程详解

2021-09-02 12:37追极 Java教程

这篇文章主要介绍了SpringCloud搭建netflix-eureka微服务集群的过程详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1.打开官网稍微学习一下,了解一下spring cloud是个什么东西,大概有哪些组件等

https://spring.io/projects/spring-cloud

https://docs.spring.io/spring-cloud-netflix/docs/current/reference/html/

2.新建项目

打开网址:https://start.spring.io/

选择需要引入的组件,然后下载下来即可

SpringCloud搭建netflix-eureka微服务集群的过程详解

3.更改项目结构

为了测试的方便,需将项目结构更改为多模块的项目。

SpringCloud搭建netflix-eureka微服务集群的过程详解

步骤如下:

(1) 依次新建子模块register-center/provider/consumer,删除父模块中多余的src、target等文件夹

(2) 修改父模块的pom文件:仅保留<dependencyManagement>配置节,<dependencies>配置节全部注释掉,因为可在子模块按需添加依赖。

SpringCloud搭建netflix-eureka微服务集群的过程详解

(3) 修改register-center的pom中的依赖配置

  1. <dependencies>
  2. <!-- Eureka注册中心 -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  6. </dependency>
  7. </dependencies>

(4) 修改provider和consumer的pom中依赖配置

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. </dependencies>

4.新建相应的测试类和配置文件

4.1 register-center模块

SpringCloud搭建netflix-eureka微服务集群的过程详解

启动类

  1. package com.hdwang.springcloudtest.registercenter;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6.  
  7. @SpringBootApplication
  8. @EnableEurekaServer
  9. public class Application {
  10.  
  11. /**
  12. * 运行点对点模式(集群模式)时,通过添加VM参数启动不同的注册中心节点实例
  13. * 例如:-Dspring.profiles.active=peer2
  14. *
  15. * @param args
  16. */
  17. public static void main(String[] args) {
  18. SpringApplication.run(Application.class, args);
  19. }
  20.  
  21. }

yml配置

  1. spring:
  2. application:
  3. #应用名
  4. name: register-center
  5. freemarker:
  6. template-loader-path: classpath:/templates/
  7. prefer-file-system-access: false
  8. #激活的配置,可运行时添加参数进行修改 -Dspring.profiles.active=peer2
  9. profiles:
  10. active: peer1
  11.  
  12. # #Eureka独立模式配置,仅有一个注册中心节点
  13. #server:
  14. # port: 8090
  15. #eureka:
  16. # instance:
  17. # hostname: localhost
  18. # client:
  19. # #仅仅作为注册中心,既不提供服务也不订阅服务
  20. # registerWithEureka: false
  21. # fetchRegistry: false
  22. # #注册中心地址
  23. # serviceUrl:
  24. # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  25.  
  26. # Eureka点对点模式,保证注册中心高可用,注册的实例信息会在点与点之间相互同步
  27. eureka:
  28. client:
  29. serviceUrl:
  30. defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/
  31.  
  32. ---
  33. #每个注册中心节点不同的配置
  34. spring:
  35. profiles: peer1
  36. server:
  37. port: 8091
  38. eureka:
  39. instance:
  40. #在本机hosts中配置即可
  41. hostname: peer1.com
  42.  
  43. ---
  44. spring:
  45. profiles: peer2
  46. server:
  47. port: 8092
  48. eureka:
  49. instance:
  50. hostname: peer2.com
  51.  
  52. ---
  53. spring:
  54. profiles: peer3
  55. server:
  56. port: 8093
  57. eureka:
  58. instance:
  59. hostname: peer3.com

4.2 provider模块

SpringCloud搭建netflix-eureka微服务集群的过程详解

启动类

  1. package com.hdwang.springcloudtest.provider;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class Application {
  8.  
  9. /**
  10. * 运行时添加VM参数: -Dserver.port=8082可以启动多个provider实例
  11. *
  12. * @param args
  13. */
  14. public static void main(String[] args) {
  15. SpringApplication.run(Application.class, args);
  16. }
  17.  
  18. }

服务注册类

  1. package com.hdwang.springcloudtest.provider.restservice;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. /**
  9. * 注册的服务
  10. */
  11. @RestController
  12. public class RestService {
  13. /**
  14. * 日志
  15. */
  16. private static final Logger LOG = LoggerFactory.getLogger(RestService.class);
  17.  
  18. @RequestMapping("/sayHello")
  19. public String sayHello(String name) {
  20. LOG.info("sayHello was called");
  21. return "hello, " + name;
  22. }
  23. }

yml配置

  1. spring:
  2. application:
  3. #应用名,也是eureka的服务名
  4. name: provider
  5. freemarker:
  6. template-loader-path: classpath:/templates/
  7. prefer-file-system-access: false
  8.  
  9. server:
  10. #运行时,添加参数-Dserver.port=8082运行新的provider实例
  11. port: 8081
  12.  
  13. eureka:
  14. client:
  15. #注册中心地址
  16. serviceUrl:
  17. #注册中心独立模式
  18. #defaultZone: http://localhost:8090/eureka/
  19. #注册中心点对点模式
  20. defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/

4.3 consumer配置

SpringCloud搭建netflix-eureka微服务集群的过程详解

启动类

  1. package com.hdwang.springcloudtest.consumer;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class Application {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(Application.class, args);
  11. }
  12.  
  13. }

服务调用测试类

  1. package com.hdwang.springcloudtest.consumer.controller;
  2.  
  3. import com.netflix.appinfo.InstanceInfo;
  4. import com.netflix.discovery.EurekaClient;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpEntity;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.util.LinkedMultiValueMap;
  11. import org.springframework.util.MultiValueMap;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import org.springframework.web.client.RestTemplate;
  15.  
  16. import java.util.*;
  17.  
  18. /**
  19. * 测试
  20. */
  21. @RestController
  22. public class TestController {
  23.  
  24. /**
  25. * 使用服务名才能负载均衡,不能使用直接使用地址
  26. */
  27. private static final String REST_URL_PREFIX = "http://provider";
  28.  
  29. @Autowired
  30. private EurekaClient discoveryClient;
  31.  
  32. @Autowired
  33. private RestTemplate restTemplate;
  34.  
  35. @GetMapping("/testGet")
  36. public String testGet() {
  37. ResponseEntity<String> res = restTemplate.getForEntity(REST_URL_PREFIX + "/sayHello?name={1}", String.class, getName());
  38. return res.getBody();
  39. }
  40.  
  41. @GetMapping("/testPost")
  42. public String testPost() {
  43. MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
  44. params.add("name", getName());
  45.  
  46. // HttpHeaders headers = new HttpHeaders();
  47. // headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  48.  
  49. HttpEntity< MultiValueMap<String, Object>> request = new HttpEntity<>(params, null);
  50. ResponseEntity<String> res = restTemplate.postForEntity(REST_URL_PREFIX + "/sayHello", request, String.class);
  51. return res.getBody();
  52. }
  53.  
  54. private String getName() {
  55. List<String> greetings = Arrays.asList("Bob", "Alice", "Jack");
  56. Random rand = new Random();
  57. int randomNum = rand.nextInt(greetings.size());
  58. return greetings.get(randomNum);
  59. }
  60. }

RestTemplate负责均衡配置类

  1. package com.hdwang.springcloudtest.consumer.config;
  2.  
  3. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.client.RestTemplate;
  7.  
  8. @Configuration
  9. public class RestTemplateConfig {
  10.  
  11. /**
  12. * 构建有负载均衡功能的RestTemplate实例对象
  13. *
  14. * @return RestTemplate实例对象
  15. */
  16. @Bean
  17. @LoadBalanced
  18. RestTemplate restTemplate() {
  19. return new RestTemplate();
  20. }
  21. }

yml配置

  1. spring:
  2. application:
  3. #应用名,也是Eureka的服务名
  4. name: cosumer
  5.  
  6. server:
  7. port: 8088
  8.  
  9. eureka:
  10. client:
  11. #注册中心地址
  12. serviceUrl:
  13. #注册中心独立模式
  14. #defaultZone: http://localhost:8090/eureka/
  15. #注册中心点对点模式
  16. defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/

5.运行测试

5.1本机hosts配置

127.0.0.1 peer1.com
127.0.0.1 peer2.com
127.0.0.1 peer3.com

5.2 编辑运行配置

SpringCloud搭建netflix-eureka微服务集群的过程详解

三个注册中心节点运行配置

SpringCloud搭建netflix-eureka微服务集群的过程详解

SpringCloud搭建netflix-eureka微服务集群的过程详解

SpringCloud搭建netflix-eureka微服务集群的过程详解

两个服务提供者的运行配置

SpringCloud搭建netflix-eureka微服务集群的过程详解

SpringCloud搭建netflix-eureka微服务集群的过程详解

5.3 运行程序

(1) 启动注册中心

依次启动RegisterCenter1->RegisterCenter2->RegisterCenter3,启动成功后,可访问http://localhost:8091/ 或http://localhost:8092/ 或http://localhost:8093/ 查看是否启动成功

SpringCloud搭建netflix-eureka微服务集群的过程详解

(2)启动服务提供者provider

依次启动Provider1->Provider2, 随便访问一个注册中心地址首页即可查看状态,如下图

SpringCloud搭建netflix-eureka微服务集群的过程详解

(3) 启动消费者cosumer

(4) 在浏览器中进行测试

测试地址:http://localhost:8088/testPost / http://localhost:8088/testGet

(5) 在Provider1/Provider2的控制台中可以看到输出结果

2021-04-07 15:26:56.043  INFO 8796 --- [nio-8081-exec-1] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:58.860  INFO 8796 --- [nio-8081-exec-2] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.535  INFO 8796 --- [nio-8081-exec-3] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.925  INFO 8796 --- [nio-8081-exec-4] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.266  INFO 8796 --- [nio-8081-exec-5] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.663  INFO 8796 --- [nio-8081-exec-6] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.938  INFO 8796 --- [nio-8081-exec-7] c.h.s.provider.restservice.RestService   : sayHello was called

2021-04-07 15:26:58.602  INFO 17828 --- [nio-8082-exec-1] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.194  INFO 17828 --- [nio-8082-exec-2] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.737  INFO 17828 --- [nio-8082-exec-3] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.109  INFO 17828 --- [nio-8082-exec-4] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.414  INFO 17828 --- [nio-8082-exec-5] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.815  INFO 17828 --- [nio-8082-exec-6] c.h.s.provider.restservice.RestService   : sayHello was called

恭喜!至此,Spring Clound 微服务集群框架您已经搭建成功!

附录

github地址:https://github.com/hdwang123/springcloudtest

参考文章:

https://www.zhihu.com/question/283286745/answer/763040709

https://www.cnblogs.com/qdhxhz/p/9357502.html

https://www.cnblogs.com/cjsblog/p/8005766.html

https://blog.csdn.net/weixin_44448094/article/details/88535475

到此这篇关于SpringCloud搭建netflix-eureka微服务集群的过程详解的文章就介绍到这了,更多相关SpringCloud搭建netflix-eureka集群内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/hdwang/archive/2021/04/07/14628372.html

延伸 · 阅读

精彩推荐