Start a spring Cloud project-Step 3-Create a service consumer

In fact, service consumers and service providers are only defined at the business level. Their identities can have two levels. For example, services A, B, and C. When A calls B, A acts as a consumer, B acts as a provider, and C When A is called, C acts as the consumer and A acts as the provider. The service A here has dual identities. Therefore, there is actually no essential difference between the provider and consumer code levels. There is only a business difference.

Create a service consumer (same steps as creating a service consumer)

Step 1: Create a new module from the parent project created in the previous step

Create module and name it consumer
Insert image description here

Insert picture description here and add provider dependency in pom.xml of the new consumer project:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

Step 2: Create a yml file and add provider configuration information

Create the application.yml file in the consumer's resource folder and add the following information:

server:
  port: 8020
spring:
  application:
      name: consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

Configuration information description:

  • server-port: port number of the service provider
  • spring-application-name: The name of the current service registered in EurekaServer
  • eureka-client-service-url-defaultZone: the address of the registration center
  • eureka-instance-prefer-ip-address: Whether to register the current service IP into Eureka Server

The third step is to configure the startup class and call information to start the service provider.

In the consumer's java folder, create a java class whose name ends with Application, and inject the RestTemplate instance into the IoC container. This is the key to enabling inter-module calls. Add the following information:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApplication {
    
    
    public static void main(String[] args){
    
    
        SpringApplication.run(ConsumerApplication.class);
    }

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

In the java folder, create a new controller file, create a ConsumerHandler class, and use the RestTemplate class to implement calls between modules.
Insert image description here

The ConsumerHandler code is as follows:

package com.southwind.controller;


import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
@RequestMapping("/consumer")
public class ConsumerHandler {
    
    
    @Autowired
    private RestTemplate restTemplate;

    //在这里通过RestTemplate的方法调用 eruekaclient 模块的方法
    //第一种方法,使用getForEntity,不是返回 ResponseEntity<T>类型,所以需要通过调用.getBody()方法
    @GetMapping("/findAll")
    public Collection<Student> findAll(){
    
    
        return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
    }
    //第二种方法,使用getForObject ,因为此方法返回值就是泛型 ,所以直接返回即可
    @GetMapping("/findAll2")
    public Collection<Student> findAll2(){
    
    
        return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
    }
    @GetMapping("/findById/{id}")
    public Student  findById(@PathVariable("id") long id){
    
    
        return restTemplate.getForEntity("http://localhost:8010/student//findById/{id}",Student.class,id).getBody();
    }

    @GetMapping("/findById2/{id}")
    public Student findById2(@PathVariable("id") long id){
    
    
        return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
    }

    @PostMapping("/save")
    public void save(@RequestBody Student student){
    
    
        restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();
    }

    @PostMapping("/save2")
    public void save2(@RequestBody Student student){
    
    
        restTemplate.postForObject("http://localhost:8010/student/save",student,void.class);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
    
    
        restTemplate.put("http://localhost:8010/student/update",student);

    }
    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
    
    
        restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
    }
}

Finally, start the registration center eurekaserver first, then start eurekaclient, then start consumer, enter http://localhost:8761 on the web page
Insert image description here

You can see that the service consumer has been registered in the registration center.
Insert picture description here
. At this point, the service consumer has been implemented.

Let me explain here that the code for service consumers and RestTemplate implementation modules above are the same. The only difference is that the consumer registers itself into the registration center, which means there is an additional yml configuration. In fact, the implementation of consumers calling providers is still implemented through RestTemplate. This is a method embedded in spring boot dependencies, and it is also the key to realizing calls between modules. This is one of the reasons why spring Cloud cannot be separated from the springboot framework.

Test it here:
Query:
Insert image description here
Test successful

Guess you like

Origin blog.csdn.net/chenmaolin928/article/details/109145882