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

一.引入Nacos注册中心

1. 引入依赖

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

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

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注解

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

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

 3.使用Ribbon

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

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

三.引入OpenFeign

1.导入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

@SpringBootApplication
@EnableFeignClients
public class OrderApp {
    public static void main(String[] args) {
        SpringApplication.run(OrderApp.class,args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

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

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

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

ProductFeign

package com.lrs.order.feign;

import com.lrs.common.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @作者:刘壬杉
 * @创建时间 2022/8/19 17:18
 **/
@FeignClient(value = "lrs-product")
public interface ProductFeign {
    @GetMapping("product/getById/{id}")
    public Product getById(@PathVariable Integer id);
}

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

3.在业务层调用该类

 四.引入Eureka注册中心

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

2.在该模块中导入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 3.编辑配置文件application.properties

server.port=7000

spring.application.name=eureka-server

#实例的主机名
eureka.instance.hostname=localhost

#是否把eureka服务端注册到注册中心
eureka.client.register-with-eureka=false

#该服务是否从注册中心拉取服务
eureka.client.fetch-registry=false

#提供类微服务的地址
eureka.client.service-url.defaultZone=http://localhost:7000/eureka

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

package com.lrs.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @作者:刘壬杉
 * @创建时间 2022/8/19 17:27
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApp.class,args);
    }
}

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

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

(1)导入依赖:

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(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包即可.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值