Introduction to the
Originally called Feign, the declarative service invocation component that the Spring Cloud team developed on top of Netflix Feign as it went open-source
HelloWorld
- 1. Prepare the provider
Make sure nacOS is started and learn nacOS by clicking juejin.cn/post/700294…
Then create the SpringBoot project Provider, which uses NACOS as the registry.
Introduction of depend on
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.4. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
application.properties
spring.application.name=provider
spring.cloud.nacos.discovery.server-addr=192.168.189.101:8848
Copy the code
Defines the interface
@RestController
public class TestController {
@Value("${server.port}")
Integer port;
@GetMapping("/hello")
public String hello(a) {
return "hello:"+ port; }}Copy the code
Once packaged, start the project with two separate ports
- 2. Ready to consumer
Create a New SpringBoot project consumer
Introduction of depend on
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.8. RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.4. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
application.properties
spring.application.name=consumer
server.port=8083
spring.cloud.nacos.discovery.server-addr=192.168.189.101:8848
Copy the code
Start the class
- 1. Enable the openFeign configuration
- 2. Define the RestTemplate to simulate load balancing
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate(a) {
return newRestTemplate(); }}Copy the code
Define the service
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("provider") // The argument is the provider's spring.application.name
public interface ConsumerService {
@GetMapping("/hello")
String hello(a); // The method name is optional
}
Copy the code
Define the controller
@RestController
public class ConsumerController {
@Autowired
ConsumerService service;
@GetMapping("/hello")
public String hello(a) {
returnservice.hello(); }}Copy the code
After starting the project, test by visiting localhost:8083/ Hello several times
Parameter passing
- 1. Parameters must be bound with parameter names
- 2. If the parameter is passed through the header, the Chinese characters must be transcoded
Continue adding interfaces using the provider
@RestController
public class TestController {
@Value("${server.port}")
Integer port;
@GetMapping("/hello")
public String hello(a) {
return "hello:" + port;
}
@PostMapping("/hello2")
public String hello2(String name) {
return name;
}
@PutMapping("/hello3")
public User hello3(@RequestBody User user) {
return user;
}
@DeleteMapping("/hello4/{id}")
public String hello4(@PathVariable int id) {
return "delete : "+ id; }}Copy the code
Repackage and run separately
The consumer’s service continues to define the interface, and the key-value parameter must specify a name
@FeignClient("provider")
public interface ConsumerService {
@GetMapping("/hello")
String hello(a); // The method name is optional
@PostMapping("/hello2")
String hello2(@RequestParam("name") String name);
@PutMapping("/hello3")
User hello3(@RequestBody User user);
@DeleteMapping("/hello4/{id}")
String hello4(@PathVariable int id);
}
Copy the code
The Consumer controller continues to define the interface
@RestController
public class ConsumerController {
@Autowired
ConsumerService service;
@GetMapping("/hello")
public String hello(a) {
return service.hello();
}
@PostMapping("/hello2")
public String hello2(String name) {
return service.hello2(name);
}
@PutMapping("/hello3")
public User hello3(@RequestBody User user) {
return service.hello3(user);
}
@DeleteMapping("/hello4/{id}")
public String hello4(@PathVariable int id) {
returnservice.hello4(id); }}Copy the code
Start Consumer to complete the test access