java version of spring cloud + spring boot + redis social e-commerce platform: service consumption (basic)

Use LoadBalancerClient
provided in Spring Cloud Commons in a large number of service-related governance abstract interfaces, including DiscoveryClient, here we are about to introduce LoadBalancerClient and so on. For definitions of these interfaces when we introduced the service on a registration and discovery have said, Spring Cloud do this level of abstraction, a good decoupling of the service management system so that we can replace the different service control facilities easily.

From the naming LoadBalancerClient interface, we know that this is an abstract definition of a client load balancing, let's look at how to use consumer Spring Cloud provides load balancing client interface to implement the service.

The following example, we will use the one built in eureka-server as a service registry, eureka-client as a service provider as a basis.
Let's create a consumer service project, named: eureka-consumer. And introducing dependency in pom.xml (dependencyManagement omitted here and the parent configuration):

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

Configuring application.properties, the specified address eureka registry:

spring.application.name=eureka-consumer
server.port=2101

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

Create a Main Class. Initialization RestTemplate, to initiate real REST request. @EnableDiscoveryClient notes for the current application to join the service governance system.

@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

Create an interface used for consumption eureka-client interfaces provided by:

@RestController
public class DcController {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String dc() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        System.out.println(url);
        return restTemplate.getForObject(url, String.class);
    }
}

You can see here, we injected LoadBalancerClient and RestTemplate, and implement / consumer interface, the first to choose a function of the load by loadBalancerClient balanced elect a eureka-client service instance, the basic information is stored in the service instance ServiceInstance and then stitching these objects through the information a detailed address to access / dc interface, and finally the use of RestTemplate object implements the interface provides the caller to the service.

After completion of the above write your code, readers can eureka-server, eureka-client, eureka-consumer are starting up, and then visit http: // localhost: 2101 / consumer , to follow-up observations eureka-consumer service is how consumption eureka-client services / dc interface.
java version of spring cloud social e-commerce platform source code, please add penguin beg: three four five three six II qi II fifty-nine

Guess you like

Origin www.cnblogs.com/summercode/p/11287304.html