This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Following up on the previous article “Getting Started with Spring Cloud Eureka and Feign (II),” which successfully launched a registry, a service provider, based on Feign to discover and consume services.
Third, Feign
This chapter introduces Feign from three aspects: first, how would we call the registry service without Feign; What is the second Feign and what does it bring us? Third, how to get started with Feign.
3.1 Without Feign
Why Feign? How can we invoke services registered with Eureka without using Feign?
Without Feign, the following core steps must be taken to ensure normal service discovery and invocation:
- Step one, use
Ribbon
Load Balancing - The second step is to get an instance of the service, get the root URL, and piece together the URL of the method
- Step 3, and finally use a REST template or other means to use the specified service
For the first step, use the Ribbon to get the service instance, which looks like this.
@Autowired
private LoadBalancerClient loadBalancer;
pulic void method(a){
ServiceInstance serviceInstance=loadBalancer.choose("producer");
}
Copy the code
For step 2, the approximate code is as follows:
pulic void method(a){
String baseUrl=serviceInstance.getUri().toString();
baseUrl=baseUrl+"/targetURL";
}
Copy the code
Finally, for step 3, perform the access and get the results, roughly as follows.
pulic void method(a){
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response=null;
try{
response=restTemplate.exchange(baseUrl,
HttpMethod.GET, getHeaders(),String.class);
}catch (Exception ex)
{
System.out.println(ex);
}
System.out.println(response.getBody());
}
Copy the code
In the whole call process, it is very complicated, we need to deal with some empty exceptions and so on, we use Feign to simplify the above steps.
3.2 introduce Feign
Feign aims to simplify the HTTP API client, which is a declarative Web Service client. Fegin is a Client binder for Java calls to HTTP, inspired by Retrofit, JAXRS-2.0, and WebSocket.
Using Feign makes it easier to write Web Service clients by defining an interface and then adding annotations to it. It also supports JAX-RS standard annotations, and Feign also supports pluggable encoders and decoders.
So how does Feign work? Simply put, Feign works by translating annotations into request templates, where parameters are simply applied directly to the template. The specific principle has not been deeply elaborated.
Using Feign, like the example provided above without using Feign, shows the call chain below.
Just like Eureka, Spring Cloud also provides an easy-to-use OpenFeign Starter, and we’ll see how Netflix Feign can be used to make service discovery and invocation easier and cleaner.
3.3 Hands-on practice
Here, we use the service registry and service provider provided in the previous article. Start by creating a new module and introducing dependencies.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code
Note that we still need to introduce the Eureka Client dependency because we need to discover the Eureka service. The next step is to provide the same interface, GreetingClient, as the service invocation.
@FeignClient("spring-cloud-eureka-client")
public interface GreetingClient {
@GetMapping("/greeting")
String greeting(a);
}
Copy the code
The problem is that the service provider and the consumer declare the same set of interfaces. Best engineering practices suggest abstracting this part of the interface as separate Modeling and then introducing the corresponding service provider and consumer into the package.
Next, write the Launcher class and the Web service. For convenience, write both to the Launcher class.
@SpringBootApplication
@EnableFeignClients
@RestController
public class Launcher {
@Autowired
private GreetingClient greetingClient;
public static void main(String[] args) {
SpringApplication.run(Launcher.class,args);
}
@GetMapping("/get-greeting")
public String greeting(a){
returngreetingClient.greeting(); }}Copy the code
Application. Yml configuration information is as follows.
spring:
application:
name: spring-cloud-eureka-feign-client
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
Copy the code
Then start the consumer service and observe the Eureka administration page. You can see that the consumer is also registered in the Eureka registry.
Then visit http://{yourhost}:8080/get-greeting to get Hello… “, which means the call chain is unblocked.
In this example, load balancing is not introduced. If you want to achieve this, you can introduce the dependency of the Ribbon and call the remote service where the code calls the service.
@Autowired private RemoteCallService loadBalancer; Data TMP = loadbalancer.getData ();Copy the code
Four,
By comparison, this whole article is relatively easy to get started with, as the series is a primer on why these distributed components are provided and what we can do with them. There are many more service registries, such as Zuul, ZK, and more, which will be covered in the next tutorial. Stay tuned!
Boy, haven’t you seen enough? Click on the stone’s home page and take a look at it casually. Maybe there will be a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!