zoukankan      html  css  js  c++  java
  • spring cloud 之demo



    springboot项目构建demo

    @EnableDiscoveryClient
    @EnableResourceServer
    @SpringBootApplication
    public class MerchantApplication {

    public static void main(String[] args) {
    SpringApplication.run(MerchantApplication.class, args);
    }
    }

    在demo的resource下面创建.yml

    spring:
    application:
    name: madmin
    profiles:
    active: default
    cloud:
    config:
    enabled: true
    uri:  指向config
    # devtools:
    # livereload:
    # port: 35730
    #debug: true

    pom.xml 添加

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <dependency>  该jar包为eureka的连接


    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    </dependency>

    代码: 远程连接eureka的服务

    @Autowired
    private DiscoveryClient discoveryClient;

    RestTemplate restTemplate = new RestTemplate();
    List<ServiceInstance> instances =
    discoveryClient.getInstances("admin");

    if (instances.size() == 0)
    return null;
    log.info(XinyuanJSONUtil.gSonStringN(instances));

    ServiceInstance t = instances.get(0);
    String url = t.getUri()+"/"+t.getServiceId().toLowerCase();
    log.info(url);
    String serviceUri = String.format("地址",
    url,id,loginType);
    ResponseEntity<BaseResultDataDTO<Authorize>> restExchange =
    restTemplate.exchange(serviceUri,HttpMethod.GET,null,new ParameterizedTypeReference<BaseResultDataDTO<Authorize>>(){},id,loginType);
    BaseResultDataDTO<Authorize> authorize = restExchange.getBody();
    log.info(XinyuanJSONUtil.gSonStringN(authorize));
    return authorize.getData();

     
     
  • 相关阅读:
    [读书笔记]子查询
    [读书笔记]SQL约束
    [转载]NoSQL数据库的基础知识
    利用C#实现对excel的写操作
    [转载]SQL Server内核架构剖析
    利用花生壳和IIS发布网页过程
    [读书笔记]ASP.NET的URL路由引擎
    [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)
    [正则表达式]基础知识总结
    CF623E Transforming Sequence
  • 原文地址:https://www.cnblogs.com/laixin09/p/9593484.html
Copyright © 2011-2022 走看看