zoukankan      html  css  js  c++  java
  • Eureka 客户端服务注册 与 消费

    pom中添加依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.8</version>
            </dependency>
        </dependencies>
    
        <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>

    application.yml

    #优雅退出配置
    management:
      endpoint:
        shutdown:
          enabled: true
      endpoints:
        web:
          exposure:
            include:
              - shutdown
              - info
              - health
    spring:
      application:
        name: eureka-provider
    server:
      port: 9090
    #服务注册
    eureka:
      client:
        service-url:
          defaultZone: http://eureka:eureka@eureka1:8761/eureka,http://eureka2:8761/eureka

    SpringCloudEurekaClientApplication.java 启动类加注解

    package com.example.springcloudeurekaclient;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class SpringCloudEurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
        }
    
    }

     消费注册表中服务:

        @Autowired
        private LoadBalancerClient loadBalancerClient;
    
        public List<User> getUser(){
            //选择调用服务的名称
            ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-provider");
            //拼接访问服务的URL
            StringBuffer sb = new StringBuffer();
            sb.append("http://").append(serviceInstance.getHost()).append(":")
                    .append(serviceInstance.getPort()).append("/getUser");
            //restTemplate请求模板
            RestTemplate restTemplate = new RestTemplate();
    
            ParameterizedTypeReference<List<User>> typeReference = new ParameterizedTypeReference<List<User>>() {};
            //封裝返回值信息
            ResponseEntity<List<User>> responseEntity = restTemplate.exchange(sb.toString(), HttpMethod.GET, null, typeReference);
            List<User> body = responseEntity.getBody();
            return body;
        }
  • 相关阅读:
    NSWindow,一些有的沒的
    IT单身男士必看【找女友单身程序员】
    Base 64 Encoding 编码
    如何成为一名优秀的C程序员
    iOS学习笔记—ObjectiveC 委托、非正式协议、正式协议
    程序员学习能力提升三要素
    工程师如何不被PM欺负
    最常被程序员们谎称读过的计算机书籍
    一些重要的算法
    asp调用.Net 托管代码
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/11102247.html
Copyright © 2011-2022 走看看