项目中nacos的使用

本文介绍了如何在SpringCloud应用中集成Nacos,首先在pom.xml添加服务注册和配置管理的依赖,接着在bootstrap.yml配置Nacos服务器地址和相关参数。然后通过@Value注解获取Nacos上的配置信息,最后展示了发送POST请求的示例代码。
摘要由CSDN通过智能技术生成

第一步:pom中添加依赖

	    <dependency>
			<!--将服务注册到nacos -->
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		</dependency>

		<!--  获取nacos上的配置信息 -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>

第二步:项目中配置nacos的地址(bootstrap.yml)

spring: 
  application:
    name: xxx    # 应用名称
  profiles:
    active: dev  # 环境配置
  cloud:
    nacos:
      username: nacos
      password: nacos
      discovery:
        server-addr: ${profile.addr}  # 服务注册地址
        namespace: ${profile.server.namespace}
      config:
        # 配置中心地址
        server-addr: ${profile.addr}
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
        namespace: ${profile.namespace}

nacos的地址信息在pom.xml文件中统一配置

<!-- 开发 -->
        <profile>
            <id>dev</id>
            <activation>
                <!--默认激活配置,maven打包默认选用的配置-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!--当前环境自定义配置,标签名自定义-->
                <profile.addr>127.0.0.1:8848</profile.addr>
                <profile.namespace>ad9e8aa0-46f9-441c-80aa-3711bd053214</profile.namespace>
                <profile.server.namespace>d17fec7f-44d5-4a03-98a0-3b95170e2619</profile.server.namespace>
            </properties>
        </profile>

第三步:获取nacos上配置的信息

@Slf4j
@Service
public class TestService
{
	// @Value 是spring自带的注解
    @Value("${sms.url}")
    private String sendSmsUrl;

    private final static String method = "POST";
    public JSONObject sendSms(UKeyProcBody uKeyProcBody) {
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30 * 1000)
                .setConnectTimeout(30 * 1000).build();
        // 创建post方式请求对象
        HttpPost post = new HttpPost(sendSmsUrl);
        post.setConfig(requestConfig);
        // 请求的数据包为raw,设置报文头为Content-Type
        post.setHeader("Content-Type", "application/json");
        Map<String, Object> paramTaskUpdate = new HashMap<String, Object>();
         
        paramTaskUpdate.put("phone", phone);
        paramTaskUpdate.put("type", "receive");
        paramTaskUpdate.put("signature", "");
        paramTaskUpdate.put("str", "");

        // 装载参数
        StringEntity postingString = new StringEntity(paramTaskUpdate.toString(), "utf-8");
        post.setEntity(postingString);
        // 执行请求并拿到结果
        HttpResponse response = null;
        String content = null;
        JSONObject resmap = new JSONObject();
        resmap.put("flag", true);
        try {
            response = httpClient.execute(post);
            // 判断返回状态是否正常
            int state = response.getStatusLine().getStatusCode();
            httpClient.close();
        } catch (Exception e) {
            return resmap;
        }
        return resmap;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值