java version of spring cloud + spring boot + redis social e-commerce platform - service consumer base

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. Social e-commerce platform source code, please add penguin beg: three four five three six II qi II fifty-nine

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.

From now on, I will be here to build process and the essence of the recent development of micro-services cloud architecture springcloud record, have a friend help more interested in spring cloud development framework, I hope to help more good scholars. We come together to explore the process of spring cloud architecture to build and how to apply enterprise projects.
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/itcode-code/p/11198737.html