Nacos AND SpringCloud微服务相关配置和使用方式

微服务


前言

微服务架构


一、配置信息

1.Nacos相关配置以及描述信息

server:
  port: 7626

spring:
  application:
    name: nacos-service-consumer
  cloud:
    discovery:
      enabled: true
    nacos:
      discovery:
        server-addr: http://localhost:8848,http://localhost:8849,http://localhost:8850 # 设置注册中心集群信息
        register-enabled: true # 是否需要进行注册到注册中心
        cluster-name: SH # 设置当前服务所在的集群

nacos-service-provider: # 获取指定的提供者服务名称
  ribbon: # 使用远程服务调用规则
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule  # nacos优先使用相同集群服务的规则,默认有限使用相同集群下的提供者服务,同集群下的提供者服务都宕机的情况下会去选择其他集群下的提供者。

2.Nacos权重配置信息

1.通过权重配置,可以实现灰度部署。当权重为0时,在nacos注册列表中会被踢出,相当于下线状态。这时我们可以重新部署项目,重新启动项目后,在设置权重。可以实现如上的应用场景。

在这里插入图片描述

3.Nacos Config 相关配置信息

1.首先需要在resources目录下创建一个bootstrap.yml文件(优先级最高,会先被先读取)。

1.文件配置格式
${prefix}-${spring.profile.active}.${file-extension}
2.bootstrap配置信息(是为了读取nacos中的配置,格式一定需要正确,否则将会提示无法获取配置文件的数据。)
spring:
  application:
    name: nacos-service-consumer
  cloud:
    nacos:
      config:
        server-addr: http://localhost:8848,http://localhost:8849,http://localhost:8850
        file-extension: yaml
        prefix: nacos-service-consumer
  profiles:
    active: dev
3.nacos中添加配置截图

在这里插入图片描述
注:Data ID的格式就是文件配置格式,需要选择YAML文件进行配置。

4.获取配置文件中代码格式,热更新注解(@RefreshScope)
@RestController
@RefreshScope // 定时刷新,热更新
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${pattern.format}")
    private String dateFormat;
    @Value("${spring.application.name}")
    private String name;

    @GetMapping("/getDateFormat")
    public String getDateFormat() {
        return new SimpleDateFormat(dateFormat).format(new Date());
    }
}

4.Cloud-OpenFeign案例教程。

1.首先需要引入OpenFeign的依赖。注意版本问题,如果你的项目的springboot依赖2.1.x开始的需要使用OpenFeign2.1.x开头的,如果springboot依赖2.2.x以上的需要使用2.2.x以上的配置,否则会出现缺少各种方法实例等问题。(博主的boot版本是2.1.x的,所以引入如下版本)

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

2.编写OpenFeign需要访问的接口。添加一个Feign的接口,并且添加OpenFeign的注解标识。接口内添加请求注解,和方法用来调用注册中心内的服务。

package com.hh.userservicenacosconsumer01.feign.service;

import com.hh.userservicenacosconsumer01.fallback.UserFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @ClassName FeignUserService
 * @Deacription TODO
 * @Author 
 * @Date 2021/12/23 13:21
 * @Version 1.0
 **/
@FeignClient(value = "nacos-service-provider", fallback = UserFallBack.class)
@Component
public interface FeignUserService {

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    String getUser();

}

3.需要在主启动类上添加OpenFeign(@EnableFeignClients)的扫描路径。也就是我们刚刚编写的接口路径下。

package com.hh.userservicenacosconsumer01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.hh.userservicenacosconsumer01.feign.service")
public class UserServiceNacosConsumer01Application {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceNacosConsumer01Application.class, args);
    }

}

4.如果刚刚细心的童鞋,应该会注意到回调方法。编写回调方法数据返回。

package com.hh.userservicenacosconsumer01.fallback;

import com.hh.userservicenacosconsumer01.feign.service.FeignUserService;
import org.springframework.stereotype.Component;

/**
 * @ClassName UserFallBack
 * @Deacription feign客户端调用接口时,数据回调,异常处理。
 * @Author 
 * @Date 2021/12/23 13:22
 * @Version 1.0
 **/
@Component
public class UserFallBack implements FeignUserService {
    @Override
    public String getUser() {
        return "未获取的用户信息。";
    }
}

5.经过以上的所有步骤后,也是亦可以使用了。但是服务还是会出现无法远程接口调用时,会出现报错异常信息。我们之前设置的fallback也没有起到效果,添加如下配置后就可以开启回调了,基本操作完成。

server:
  port: 7626

spring:
  application:
    name: nacos-service-consumer
  cloud:
    discovery:
      enabled: true
    nacos:
      discovery:
        server-addr: http://localhost:8848,http://localhost:8849,http://localhost:8850 # 设置注册中心集群信息
        register-enabled: true # 是否需要进行注册到注册中心
        cluster-name: SH # 设置当前服务所在的集群

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 2000
        readTimeout: 2000

nacos-service-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule  # nacos优先使用相同集群服务的规则,默认有限使用相同集群下的提供者服务,同集群下的提供者服务都宕机的情况下会去选择其他集群下的提供者。

总结

记录微服务相关信息,使用规则,后续会继续完善。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉忆-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值