Spring Cloud introductory notes (three) declarative service call using Feign of

Paper notes on the basis of the following:

In the last notes of the RestTemplate to make the call between the service and found that write a lot of trouble, each time by themselves to get Url DiscoveryClient, configuration data type, but also the assembly parameters, if the interface to more than code for highly repetitive, then down will use openFeign to achieve.

Make changes directly on the notes (b) of the project, the project is structured as follows:

 In the consumer project pom.xml file to add a openFeign dependent:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

Profile unchanged but need to add @EnableFeignClients comment on the boot class consumer to open Feign support, and remove the last of the bean RestTemplat

@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {

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

Then declare an interface used to bind the service provider provides, as follows:

@FeignClient("provider")
public interface RequestService {
    @RequestMapping("/sayHello")
    String sayHello(@RequestParam("name") String name);
}

Use to @FeignClient ( "provider") annotation to the current service provider interface binding, which is privider service name in / sayHello plus in front of parameters @RequestParam ( "name") notes that must be added to of. We then injected in Controoler RequestService interface used to remove RequestService will take the interface-related services, Controller modified as follows:

@RestController
public class TestController {

    @Autowired
    RequestService requestService;

    @RequestMapping("/sayHello")
    public String sayHello(String name){
        return requestService.sayHello(name);
    }
}

With respect to the Notes (b) the use RestTemplate way to request is not a lot of convenience?

Then start the project to access what / sayHello interface returns the following:

I can see is the result of notes and (b) is the same, but a lot of convenience.

Parameter passing

Next, look at the basic recording using the passed parameters, the parameters in the development of our meter is nothing more than passing key / value in the form of parameters, some on the request body (body), some on the request header (header), a some on the url, the next four primary transfer parameters recorded in the above form.

First recommended a new sub-project called entity in the cloud project, and then create a new entity in the entity class User.java project

public class User {
    private String uid;
    private String name;
    private Integer age;

    public User() {
    }
    public User(String uid, String name, Integer age) {
        this.uid = uid;
        this.name = name;
        this.age = age;
    }
    //这里省略了get/set和toStirng方法自己加上
}

NOTE: If you add parameters must be configured with add constructor with no arguments, or will be error

Then the provider and consumer pom.xml in the project to the introduction of dependent entity of the project

        <dependency>
            <groupId>com.example.test</groupId>
            <artifactId>entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

Next licking privider is added Controller interface: simple point to these interfaces are not interact with the database, but the return point data to facilitate testing.

import com.example.test.cloud.entity.User;
@RestController
class TestController {

    @PostMapping("/user")
    public User addUser(@RequestBody User user){
        return user;
    }
    @GetMapping("/user")
    public User getUser(@RequestParam String name){
        return new User("123456",name,24);
    }
    @DeleteMapping("/user/{uid}")
    public String deleteByUser(@PathVariable String uid){
        return uid+"已经删除";
    }
    @PutMapping("/user")
    public String updateUserByUid(@RequestHeader String uid){
        return uid+"更新成功";
    }
}

Then sonsumer project RequestService interface licking the following interfaces:

@FeignClient("provider")
public interface RequestService {

    @PostMapping("/user")
    public User addUser(@RequestBody User user);
    @GetMapping("/user")
    public User getUser(@RequestParam String name);
    @DeleteMapping("/user/{uid}")
    public String deleteByUser(@PathVariable String uid);
    @PutMapping("/user")
    public String updateUserByUid(@RequestHeader String uid);
}

 Next we injected RequestService Interface Controller class consumer's interface can then call the code as follows:

@RestController
public class TestController {
    @Autowired
    RequestService requestService;
    @RequestMapping("/test")
    public void test(){
        User user = new User("123456","张某某",24);
        System.out.println(requestService.addUser(user));
        System.out.println(requestService.getUser("xiabai"));
        System.out.println(requestService.deleteByUser("1234567"));
        System.out.println(requestService.updateUserByUid("1234678"));
    }
}

Then access / test interfaces in the browser. And then view the log output results are as follows

 

Published 22 original articles · won praise 140 · views 90000 +

Guess you like

Origin blog.csdn.net/zzqaaasss/article/details/104319360
Recommended