java version of spring cloud + spring boot 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 LoadBalancerClientso 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 LoadBalancerClientname of the interface, we know this is an abstract definition of a client load balancing, let's look at how to use Spring Cloud provides load balancing client interface to implement consumer services.

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 pom.xmlintroducing dependencies (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>
    • Configuration 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. @EnableDiscoveryClientNotes for the current application to join the service governance system.
      • @EnableDiscoveryClient
        @SpringBootApplication
        public class Application {
        
            @Bean
            public Residual Template rest template () {
                 return  new Rest Template ();
            }
        
            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
              Rest Template rest template;
          
              @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);
              }
          }

          Spring cloud b2b2c 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/springfresh/p/11041061.html