j9九游会登录/ 应用平台 appstage/ sdk参考/ / / 使用cloud map sdk(spring cloud框架)
更新时间:2025-01-20 gmt 08:00

使用cloud map sdk(spring cloud框架)-j9九游会登录

引入cloud map sdk

  1. 引入sts

    cloud map依赖sts认证能力,接入cloud map必须接入sts,具体请参考引入sts sdk

  2. 引入cloud map

    在pom.xml中添加cloud map sdk依赖。

    • 将${cloudmap-sdk-version}替换成实际所使用的cloud map sdk版本。
    • 如果将sdk放到外部maven仓中,则只需要添加nuwa-cloudmap-core依赖。
    • 如果采用本地依赖的方式引入sdk,即手动将本地下载的sdk jar包引入到工程的lib目录下,还需要添加间接依赖:nuwa-cloudmap-spring-boot-starter、spring-cloud-starter-cloudmap-discovery。
      
         com.huawei.wisecloud.nuwa
         nuwa-cloudmap-core
         ${cloudmap-sdk-version}
      

配置cloud map

在微服务的application.yaml配置文件中添加以下配置项:

nuwa:
  cloudmap:
    read: cloudmap                              #使用cloud map方式进行微服务间通信
    clustername: clustername-example            #微服务注册到cloud map的集群 
    provider:
      cluster: clustername-example              #提供服务的其他同样被注册到cloud map的微服务集群名 
    serveraddr: http://10.34.32.243:80          #cloud map访问地址
    version: 1.0.0.100                          #微服务版本号
    namespacename: cn_dev_default               #cloud map访问命名空间

初始化cloud map

在启动类中增加@enablediscoveryclient注解,同时在启动类中完成将resttemplate放到spring容器中,后续微服务间调用就使用注册到spring容器中的resttemplate,代码如下:

package com.huawei.demo.servicea;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.cloud.client.loadbalancer.loadbalanced;
import org.springframework.context.annotation.bean;
import org.springframework.web.client.resttemplate;
import com.huawei.wisesecurity.sts.springboot.security.annotation.enablestsautoinitialization;
@springbootapplication
@enablestsautoinitialization(value = "application.properties")
@enablediscoveryclient
public class serviceaspringbootapplication {
    public static void main(string[] args) {
        springapplication.run(serviceaspringbootapplication.class, args);
    }
    @bean
    @loadbalanced
    public resttemplate resttemplate() {
        return new resttemplate();
    }
}

微服务间调用:

public orderinfo findorder(string orderid, httpservletrequest request) {
        orderinfo orderinfo = ordermapper.getorderbyid(orderid);
        string url = demoserviceaurl   "/user/"   orderinfo.getuserid();
        httpheaders headers = createheaders(request);
        httpentity entity = new httpentity<>(null, headers);
        responseentity responseentity = resttemplate.exchange(url, httpmethod.get, entity, userinfo.class); ;
        if (responseentity != null) {
            orderinfo.setuserinfo(responseentity.getbody());
        }
        return orderinfo;
    }

相关文档

网站地图