0
点赞
收藏
分享

微信扫一扫

SpringBoot使用Nacos服务发现与配置《微服务》

香小蕉 2023-01-09 阅读 129


1.nacos简介

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

2. pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.lys.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-provider-payment8001</artifactId>


<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--<version>2.2.1.RELEASE</version>-->
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.lys.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

3.bootrap.yml 配置(bootrap.yml优先于application.yml)

server:
port: 8001

nacos:
config:
bootstrap:
log:
enable: true

spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 10.0.1.1:8848 #Nacos作为服务注册中心地址
config:
server-addr: 10.0.1.1:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
group: DEFAULT_GROUP #指定在哪个group里
namespace: f3e27ebf-0294-4ca9-b2a3-f2a327433c0e
# {spring.application.name}-{spring.profiles.active}.{spring.cloud.nacos.config.file-extension}
# 实际访问的配置nacos-payment-provider.yaml

3.NacosProviderApplication

package com.lys;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* @Auther: liuysh
* @Date: 2021/2/27 17:51
* @Description:
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}

@RestController
class EchoController {
@GetMapping(value = "/payment/get/{id}")
public String echo(@PathVariable String id) {
return "Hello Nacos Discovery " + id;
}
}
}

4.效果

SpringBoot使用Nacos服务发现与配置《微服务》_ide

5.配置中心

package com.lys.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @Auther: liuysh
* @Date: 2021/4/12 15:54
* @Description:
*/
@RestController
@RefreshScope
public class ConfigController {
@Value("${info}")
private String configInfo;

@GetMapping("/info")
public String getConfigInfo() {
return configInfo;
}
}

SpringBoot使用Nacos服务发现与配置《微服务》_maven_02

6.调用

SpringBoot使用Nacos服务发现与配置《微服务》_maven_03

源码请私信


举报

相关推荐

0 条评论