使用Nacos作為配置中心和注冊中心
-
- 使用Nacos作為配置中心
- 使用Nacos作為注冊中心
- 使用feign進行服務間的調用
- 消費者子產品配置Nacos的負載均衡政策
使用Nacos作為配置中心
在項目中引入依賴
<properties>
<springboot.version>2.2.5.RELEASE</springboot.version>
<nacos.version>2.2.0.RELEASE</nacos.version>
<openfeign.version>2.2.2.RELEASE</openfeign.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${openfeign.version}</version>
</dependency>
<!-- Nacos -->
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- 略 -->
</dependencies>
</dependencyManagement>
添加配置檔案
bootstrap.yml
server:
port: 8881
spring:
application:
name: nacos-demo-provider
profiles:
active: dev
cloud:
nacos:
config:
# 本機
namespace: 3f1e9911-0b9a-415f-a757-09368408eaa9
server-addr: localhost:8848 #Nacos位址
file-extension: yaml #這裡我們擷取的yaml格式的配置
nacos:
@RefreshScope
注解可以使
@Value("${data.code}")
的值動态重新整理
使用Nacos作為注冊中心
server:
port: 8881
spring:
application:
name: nacos-demo-provider
profiles:
active: dev
cloud:
nacos:
config:
# 本機
namespace: 3f1e9911-0b9a-415f-a757-09368408eaa9
server-addr: localhost:8848 #Nacos位址
file-extension: yaml #這裡我們擷取的yaml格式的配置
#服務發現
discovery:
# 本機
namespace: 3f1e9911-0b9a-415f-a757-09368408eaa9
server-addr: localhost:8848 #Nacos位址
weight: 1
//啟動類加上 @EnableDiscoveryClient 注解
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDemoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDemoServiceApplication.class, args);
}
}
啟動項目即注冊到nacos上
使用feign進行服務間的調用
在API子產品中定義
FeignClient
@FeignClient(name = "nacos-demo-provider",
fallbackFactory = DemoServiceFallback.class)
@RequestMapping("/feign_service")
public interface DemoService {
@PostMapping("/server/say")
String say();
}
配置熔斷政策需要開啟
hystrix
,進入到
nacos
管理背景,添加
nacos-demo-consumer.yaml
的配置
feign:
hystrix:
enabled: true
@Component
public class DemoServiceFallback implements FallbackFactory<DemoService> {
@Override
public DemoService create(Throwable throwable) {
return () -> "調用失敗啦";
}
}
配置
spring.factories
使API子產品中的Spring注解生效
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kenny.api.service.DemoServiceFallback
在提供者子產品中實作
DemoService
@RestController
public class DemoServiceImpl implements DemoService {
@Override
public String say() {
return "服務被調用---111";
}
}
在消費者者子產品中調用
DemoService
//指定FeignClients 掃描包的路徑(不再同一個子產品内需配置 basePackages = {"xxx"})
@EnableFeignClients(basePackages = {"com.kenny"})
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDemoConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDemoConsumerApplication.class, args);
}
}
@RestController
@RequestMapping("/demo")
public class ConsumerController {
@Autowired
private DemoService demoService;
@PostMapping("consumer")
public void userService() {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 50; i++) {
String say = demoService.say();
list.add(say);
}
long one = list.stream().filter(o -> StringUtils.contains(o, "111")).count();
long two = list.stream().filter(o -> StringUtils.contains(o, "222")).count();
long three = list.stream().filter(o -> StringUtils.contains(o, "333")).count();
long fail = list.stream().filter(o -> StringUtils.contains(o, "失敗")).count();
System.out.printf("111 --》 %s 次\n", one);
System.out.printf("222 --》 %s 次\n", two);
System.out.printf("333 --》 %s 次\n", three);
System.out.printf("fail --》 %s 次\n", fail);
}
}
消費者子產品配置Nacos的負載均衡政策
因為
LoadBalancerClient
、
Feign
和
Ribbon
3種方式,它們的底層都是使用Ribbon做負載均衡的,而
Ribbon
負載均衡預設使用的政策是
ZoneAvoidanceRule
,我們要修改
Ribbon
的預設政策,讓它使用
nacos
的權重,那麼該如何配置呢?
我們進入到
nacos
管理背景,添加
nacos-demo-consumer.yaml
的配置,添加如下配置:
# 以下三行配置 指定 ribbon 使用nacos 的負載均衡政策 (配在 consumer 中)
nacos-demo-provider: #服務的名稱
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
配置提供者的權重
消費者調用執行個體
源碼位址
參考:
Nacos配置中心和服務的注冊發現
芋道 Spring Cloud Netflix 負載均衡 Ribbon 入門