首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

SpringCloud项目Eureka快速入门

  • 25-04-24 08:47
  • 2733
  • 10749
blog.csdn.net

一、引言

        在正式开始学习之前我们需要先掌握两个微服务中的概念,服务提供者(Service Provider)和服务消费者(Service Consumer),我们知道在微服项目中,由于服务的职责单一性,数据的独立性,我们需要其他数据库数据时,需要发送HTTP请求跨服务读取另一数据库的内容。此时被请求的一方即提供者,请求方为消费者。

        而面对大量的跨服务请求我们如果手动在各个服务里硬编码HTTP请求路径将非常不利于集群的开发和维护,我们今天将使用Eureka解决这一问题。

 二、Eureka简介

        Spring Cloud Eureka 是 Netflix 开源的一个服务发现框架,它被集成到了 Spring Cloud 中,提供了服务注册与发现的功能。在微服务架构中,服务实例可能会动态地增加或减少,服务发现机制允许各个服务实例能够互相感知对方的存在。Spring Cloud Eureka 提供了一个简单而强大的服务注册与发现机制,使得在微服务架构中管理和调用服务变得更加容易。

 三、入门项目

1.创建Eureka服务端:

  • 创建一个Maven工程,用于搭建Eureka服务端。
  • 在pom.xml文件中导入Eureka Server的依赖。
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloudgroupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
  5. dependency>
  6. dependencies>
  • 在application.yml文件中配置Eureka Server的相关信息。
  1. server:
  2. port: 7071
  3. # 注册eureka服务
  4. spring:
  5. application:
  6. name: eurekaserver #eureka的服务器名称
  7. eureka:
  8. client:
  9. service-url: #eureka的地址信息
  10. defaultZone: http://localhost:10086/eureka
  11. register-with-eureka: false # 不向注册中心注册自己
  12. fetch-registry: false # 不从注册中心抓取服务
  • 创建主启动类,并在主启动类上添加 @EnableEurekaServer 注解,以启动Eureka Server。
    1. @EnableEurekaServer
    2. @SpringBootApplication
    3. public class EurekaServerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(EurekaServerApplication.class, args);
    6. }
    7. }
  • 启动Eureka注册中心。然后,可以通过浏览器访问 http://localhost:7001/ 来查看Eureka注册中心的界面。

 2.搭建服务提供者:

  • 创建一个新的Maven工程,用于搭建服务提供者。
  • 在pom.xml文件中,导入Eureka Client的依赖和Spring Boot的启动依赖等。
    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>mysqlgroupId>
    7. <artifactId>mysql-connector-javaartifactId>
    8. dependency>
    9. <dependency>
    10. <groupId>org.mybatis.spring.bootgroupId>
    11. <artifactId>mybatis-spring-boot-starterartifactId>
    12. dependency>
    13. <dependency>
    14. <groupId>org.springframework.cloudgroupId>
    15. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    16. dependency>
  • 在application.yml文件中,配置服务提供者的相关信息(主要配置为spring.application.name和eureka)。
    1. server:
    2. port: 8081
    3. spring:
    4. datasource:
    5. url:
    6. username:
    7. password:
    8. driver-class-name: com.mysql.jdbc.Driver
    9. application:
    10. name: userservice
    11. mybatis:
    12. type-aliases-package: cn.cds.user.pojo
    13. configuration:
    14. map-underscore-to-camel-case: true
    15. logging:
    16. level:
    17. cn.itcast: debug
    18. pattern:
    19. dateformat: MM-dd HH:mm:ss:SSS
    20. eureka:
    21. client:
    22. service-url: #eureka的地址信息
    23. defaultZone: http://localhost:7071/eureka
  • 创建Controller,用于对外提供服务。
    1. import cn.cds.user.pojo.User;
    2. import cn.cds.user.service.UserService;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.*;
    6. @Slf4j
    7. @RestController
    8. @RequestMapping("/user")
    9. public class UserController {
    10. @Autowired
    11. private UserService userService;
    12. /**
    13. * @param id 用户id
    14. * @return 用户
    15. */
    16. @GetMapping("/{id}")
    17. public User queryById(@PathVariable("id") Long id) {
    18. return userService.queryById(id);
    19. }
    20. }
  •  在主启动类上添加 @EnableEurekaClient 注解,以启用Eureka Client。
    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.mybatis.spring.annotation.MapperScan;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. @MapperScan("cn.cds.user.mapper")
    6. @SpringBootApplication
    7. @EnableEurekaClient
    8. public class UserApplication {
    9. public static void main(String[] args) {
    10. SpringApplication.run(UserApplication.class, args);
    11. }
    12. }

3.创建服务消费者:

        重复服务提供者搭建的所有操作,做一个用于请求服务提供者的接口,不同的是,为了模拟实际业务场景,我们所配置的数据库和端口皆不同于已创建的其他服务。

  1. import cn.cds.order.pojo.Order;
  2. import cn.cds.order.service.OrderService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("order")
  10. public class OrderController {
  11. @Autowired
  12. private OrderService orderService;
  13. @GetMapping("{orderId}")
  14. public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
  15. // 根据id查询订单并返回
  16. return orderService.queryOrderById(orderId);
  17. }
  18. }

并且在Service层不再需要硬编码请求地址,改为注册中心中的服务器名即可 

  1. import cn.cds.order.mapper.OrderMapper;
  2. import cn.cds.order.pojo.Order;
  3. import cn.cds.order.pojo.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.client.RestTemplate;
  7. @Service
  8. public class OrderService {
  9. @Autowired
  10. private OrderMapper orderMapper;
  11. @Autowired
  12. private RestTemplate restTemplate;
  13. public Order queryOrderById(Long orderId) {
  14. // 1.查询订单
  15. Order order = orderMapper.findById(orderId);
  16. //2.利用RestTemplate发起http请求,查询用户
  17. //注意此处url
  18. String url="http://userservice/user/"+order.getUserId();
  19. User user = restTemplate.getForObject(url, User.class);
  20. //3.封装user到Order
  21. order.setUser(user);
  22. // 4.返回
  23. return order;
  24. }
  25. }

此时我们再通过浏览器访问 http://localhost:7001/ 来查看Eureka注册中心的界面,userservice和orderservice均以注册。

4.Ribbon负载均衡测试

        另外我们再扩展一小部分,我们知道,在真实的业务场景中一个服务可能对应多个实例或服务器,在微服务架构中,负载均衡通常用于服务网关,以分发进入的API请求到后端的微服务实例。

  • 我们再复制一个userservice

  •  启动UserApplication2再去浏览器的注册中心查看,此时列表中有两个userservice
  • 在服务消费者的配置类中为RestTemplate添加@LoadBalanced注解,使拦截器对其拦截并进行IRule规则的负载均衡
    1. /**
    2. * 创建RestTemplate并注入IOC容器,发送http请求
    3. */
    4. @LoadBalanced
    5. @Bean
    6. public RestTemplate restTemplate(){
    7. return new RestTemplate();
    8. }
  • 负载均衡测试

        我们两次调用服物消费者所在接口,可以看到两次请求分别根据负载均衡规则请求到了8082和8081端口

 

注:本文转载自blog.csdn.net的东山-CDS的文章"https://blog.csdn.net/2301_77251217/article/details/146102761"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

111
大数据
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top