Eureka registered client service and consumer

pom add a dependent

<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 startup class annotated

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);
    }

}

 Consumer registry services:

    @Autowired
     Private LoadBalancerClient loadBalancerClient; 

    public List <the User> getUser () {
         // choose to call the name of the service 
        ServiceInstance ServiceInstance = loadBalancerClient.choose ( "Eureka-Provider" );
         // stitching access services the URL of 
        StringBuffer SB = new new StringBuffer () ; 
        sb.append ( "HTTP: //"). .append (serviceInstance.getHost ()) the append ( ":" ) 
                . .append (serviceInstance.getPort ()) the append ( "/ the getUser" );
         // RestTemplate request template 
        RestTemplate RestTemplate = new 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;
    }

 

Guess you like

Origin www.cnblogs.com/ShaoXin/p/11102247.html