Springcloud (4) Declaring a trial service call feign

Source: Internet
Author: User

Use Fegin to invoke the service:

First modify the two Hello-server services to add

@GetMapping ("/hello1")
public string Hello (@RequestParam string name) {
Return "Hello" + name;
}

@GetMapping ("/hello2")
Public User Hello (@RequestHeader String name, @RequestHeader the Integer age) {
return new User (name,age);
}

@PostMapping ("/hello3")
Public String Hello (@RequestBody user user) {
Return "Hello" +user.getname () + "," +user.getage ();
}
User entity class
public class User implements serializable{

Private static final long serialversionuid = 2066603420531796503L;

private String name;
Private Integer age;

Public User () {
}

Public User (String name, Integer age) {
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public Integer Getage () {
return age;
}

public void Setage (Integer age) {
This.age = age;
}
}


First, we introduce POM dependency

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

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
Then add the following annotations to the application:
@EnableDiscoveryClient
@EnableFeignClients

Application.properties configuration file
server.port=9001
Spring.application.name=feign-consumer
eureka.client.service-url.defaultzone=http://localhost:8877/eureka/,http://localhost:8898/eureka/

Writing feign clients
 @FeignClient ("Hello-server") 
Public interface Feignservice {

@RequestMapping ("/hello")
String He Llo ();

@GetMapping ("/hello1")
String Hello (@RequestParam ("name") string name);

@GetMapping ("/hello2")
User Hello (@RequestHeader ("name") String name, @RequestHeader ("age") of Integer age);
@PostMapping ("/hello3")
String Hello (@RequestBody user user);

}

Control layer Invoke demo
@RestController
public class Feigncontroller {

@Resource
Private Feignservice Feignservice;

@RequestMapping ("/feign")
Public String feign () {
return Feignservice.hello ();
}

@RequestMapping ("/feign2")
Public String feign2 () {
String str1 = Feignservice.hello ("goblin");
String str2 = Feignservice.hello ("Goblin", "N"). ToString ();
String STR3 = Feignservice.hello (New User ("Goblin", 22));
Return str1 + "+ str2 +" "+ str3;
}

}

=========================================== to be supplemented ============================================

Springcloud (4) Declaring a trial service call feign

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.