• Springcloud------Nacos&Ribbon&OpenFeign or Eureka


    一.引入Nacos注册中心

    1. 引入依赖

    需要注册的一方通常都是被调取的一方,我们称之为生产者,以及需要调取的一方,我们称之为消费者,那么这两者都需要通过注册中心去完成调用和被调用的过程.所以谁用到了Nacos,谁就需要导入Nacos的依赖

    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
    

    2.编辑配置文件application.properties

    #访问nacos
    spring.cloud.nacos.discovery.server-addr=localhost:8848
    #设置实例名称
    spring.application.name=lrs-order
    #设置不注册  该配置只需在调取的一方中加入,即消费者中加入
    spring.cloud.nacos.discovery.register-enabled=false 
    

    3. 启动Nacos

    在nacos所在的目录下的bin中打开cmd窗口并输入以下内容让其以单机模式启动:

    startup.cmd -m standalone

    4.使用Nacos

    通过DiscoveryClient类,我们可以调取服务地址来替换手写url路径

    二.引入Ribbon

    1.在RestTemplate的配置上加一个@LoadBalance注解

    1. @Bean
    2. @LoadBalanced
    3. public RestTemplate restTemplate(){
    4. return new RestTemplate();
    5. }

    2.配置类中编辑负载均衡的策略

     Ribbon中的策略模式有以下几种:

     3.使用Ribbon

    我们使用了Ribbon以后,地址也不需要通过DiscoveryClient类来获取url再拼接了,我们只需要将其中的路径换位被请求模块在注册中心中的实例名称.

     并且在集群化模式下,这种访问模式.会自动帮我们按照我们设定好的负载均衡策略去调用对应的模块所在服务器

    三.引入OpenFeign

    1.导入依赖

    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    

    2.在主启动类上开启Feign的注解

    1. @SpringBootApplication
    2. @EnableFeignClients
    3. public class OrderApp {
    4. public static void main(String[] args) {
    5. SpringApplication.run(OrderApp.class,args);
    6. }
    7. @Bean
    8. @LoadBalanced
    9. public RestTemplate restTemplate(){
    10. return new RestTemplate();
    11. }
    12. }

    2.创建OpenFeign对应的伪客户端类

    想要调用谁,就给谁做伪客户端,就好像将跨服务调用变得像本地调用一样丝滑

    该类上一定要加入@FeignClient注解,其中的value值为对应服务(也就是被调用的服务)的实例名称

    ProductFeign

    1. package com.lrs.order.feign;
    2. import com.lrs.common.entity.Product;
    3. import org.springframework.cloud.openfeign.FeignClient;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PathVariable;
    6. /**
    7. * @作者:刘壬杉
    8. * @创建时间 2022/8/19 17:18
    9. **/
    10. @FeignClient(value = "lrs-product")
    11. public interface ProductFeign {
    12. @GetMapping("product/getById/{id}")
    13. public Product getById(@PathVariable Integer id);
    14. }

    并且路径要和原服务上的路径相同,方法的返回类型也要相同,传入的参数也要相同.缺一不可

    3.在业务层调用该类

     四.引入Eureka注册中心

    1.创建一个eureka-server服务模块

    2.在该模块中导入依赖

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

     3.编辑配置文件application.properties

    1. server.port=7000
    2. spring.application.name=eureka-server
    3. #实例的主机名
    4. eureka.instance.hostname=localhost
    5. #是否把eureka服务端注册到注册中心
    6. eureka.client.register-with-eureka=false
    7. #该服务是否从注册中心拉取服务
    8. eureka.client.fetch-registry=false
    9. #提供类微服务的地址
    10. eureka.client.service-url.defaultZone=http://localhost:7000/eureka

     4.该模块中建立主启动类并开启EurekaServer注解

    1. package com.lrs.eureka;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    5. /**
    6. * @作者:刘壬杉
    7. * @创建时间 2022/8/19 17:27
    8. **/
    9. @SpringBootApplication
    10. @EnableEurekaServer
    11. public class EurekaApp {
    12. public static void main(String[] args) {
    13. SpringApplication.run(EurekaApp.class,args);
    14. }
    15. }

    5.启动该模块,输入地址打开注册中心

     6.调用模块与被调用模块的配置

    (1)导入依赖:

    注意:别导错了,这个是client客户端,server模块中是server服务端

    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    

    (2)有关配置类

    • 不论是调用这还是被调用者,都需要写自己的实例名称spring.application.name
    • 都需要写注册中心的url路径:   eureka.client.service-url.defaultZone=http://localhost:7000/eureka
    • 如果是调用者,则不需要注册到注册中心,只需要调用,那么要加上该配置:  
      eureka.client.register-with-eureka=false

    (3)OpenFeign中自带有Ribbon的jar,所以使用eureka一样可以配合OpenFeign和Ribbon,只需要导入OpenFeign的jar包即可.

  • 相关阅读:
    什么是指针?
    一个简单高效低内存的.NET操作Excel开源框架 - MiniExcel
    【附源码】计算机毕业设计JAVA重工教师职称管理系统
    时间任务管理软件OmniFocus 3 mac中文版软件特色
    pandas使用dataframe中的两列时间对象数据列作差生成时间差数据列、筛选dataframe数据中时间差(timedelta对象)大于指定阈值的数据行
    如何找回回收站清空的文件?
    Pytorch数据集读出到transform全过程
    Java项目硅谷课堂学习笔记-P5讲师管理模块前端
    MyBatis-Plus 系列
    Python传参拷贝问题
  • 原文地址:https://blog.csdn.net/lrs998563/article/details/126431100