当前位置:   article > 正文

项目中nacos的使用_新项目中使用nacos

新项目中使用nacos

第一步: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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第二步:项目中配置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}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

第三步:获取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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号