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