Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Eureka to build a simple server registration service

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Eureka to build a simple server registration service". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to build a simple server registration service using Eureka".

There are three roles in the case: the service registry, the service provider, and the service consumer, in which the service registry is the Eureka single-node startup we used in the previous article.

The process is as follows:

Start the registry

The service provider produces the service and registers with the service center

Consumers get services from the service center and execute

Service provider

Let's assume that the service provider has a hello () method that provides a service that outputs "hello xxx + current time" based on the parameters passed in.

POM package configuration

Create a basic Spring Boot application named eureka-producer, and add the following configuration to pom.xml:

Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client

Configuration file

The application.yml configuration is as follows

Spring: application: name: eureka-producereureka: client: service-url: defaultZone: http://localhost:7000/eureka/server: port: 8000

Through the spring.application.name attribute, we can specify the name of the microservice and then use that name to access the service when invoked. The eureka.client.serviceUrl.defaultZone attribute specifies the location of the service registry, corresponding to the configuration content of the service registry. To test the DiffServ provider and service registry on the local machine, use the server.port property to set different ports.

Startup class

As long as it is generated by default, there is no need to add the @ EnableDiscoveryClient annotation for this version of Finchley.RC1. So what if I introduce the relevant jar package and want to disable service registration and discovery? Set eureka.client.enabled=false)

@ EnableDiscoveryClient is no longer required. You can put a DiscoveryClient implementation on the classpath to cause the Spring Boot application to register with the service discovery server.

Spring Cloud-@ EnableDiscoveryClient

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

After starting the project, you can see the EUERKA-PRODUCER service on the page of the registry Eureka.

Service consumers

There are three ways to create a service consumer, depending on how you use API. Although you should all use Feign in practice, let's introduce all three here. If you only care about Feign, you can skip to the end.

All three ways use the same profile and are no longer explained separately

Spring: application: name: eureka-consumereureka: client: service-url: defaultZone: http://localhost:7000/eureka/ # specifies the address of the Eureka registry server: port: 9000 # is 9000, 9001 and 9002 respectively

Use LoadBalancerClient

From the naming of the LoadBalancerClient interface, we know that this is an abstract definition of a load balancer client. Let's take a look at how to use the load balancer client interface provided by Spring Cloud to achieve service consumption.

POM package configuration

Let's start by creating a service consumer project named eureka-consumer. Pom.xml is the same as Producer, I will not repeat it.

Startup class

Initialize RestTemplate, which is used to initiate a REST request.

@ SpringBootApplicationpublic class EurekaConsumerApplication {@ Bean public RestTemplate restTemplate () {return new RestTemplate ();} public static void main (String [] args) {SpringApplication.run (EurekaConsumerApplication.class, args);}}

Controller

Create an interface to consume the interface provided by eureka-producer:

RequestMapping ("/ hello") @ RestController public class HelloController {@ Autowired private LoadBalancerClient client; @ Autowired private RestTemplate restTemplate; @ GetMapping ("/") public String hello (@ RequestParam String name) {name + = "!"; ServiceInstance instance = client.choose ("eureka-producer"); String url = "http://" + instance.getHost () +": "+ instance.getPort () +" / hello/?name= "+ name Return restTemplate.getForObject (url, String.class);}}

As you can see here, we inject LoadBalancerClient and RestTemplate, and in the hello method, we first use the choose method of loadBalancerClient to load balance to select a service instance of eureka-producer. The basic information of this service instance is stored in ServiceInstance, and then the detailed address of the caller's / hello/ interface is spliced through the information in these objects. Finally, the RestTemplate object is used to call the service provider interface.

In addition, in order to distinguish the result of the call from that of the service provider, I simply deal with name+= "!", that is, the service caller's response will have an exclamation point (!) than the service provider's.

Spring Cloud Ribbon

Ribbon, which is a client-side load balancer based on HTTP and TCP, has been introduced earlier. It can balance the load by configuring ribbonServerList in the client to set the server list to poll access.

When Ribbon is used in conjunction with Eureka, ribbonServerList is rewritten by DiscoveryEnabledNIWSServerList to expand to obtain a list of service instances from the Eureka registry. It also replaces IPing with NIWSDiscoveryPing, which delegates responsibility to Eureka to determine whether the server has been started.

POM package configuration

Make a copy of the previous eureka-consumer project and name it eureka-consumer-ribbon.

Just use the previous pom.xml file. As for spring-cloud-starter-ribbon, because the Spring Cloud version I use is Finchley.RC1,spring-cloud-starter-netflix-eureka-client, it already includes spring-cloud-starter-netflix-ribbon.

Org.springframework.cloud spring-cloud-starter-netflix-eureka-client

Startup class

Modify the application main class to add @ LoadBalanced annotation to RestTemplate

@ LoadBalanced@Beanpublic RestTemplate restTemplate () {return new RestTemplate ();}

Controller

Modify controller, remove LoadBalancerClient, and modify the corresponding method to initiate a request directly with RestTemplate

@ GetMapping ("/") public String hello (@ RequestParam String name) {name + = "!"; String url = "http://eureka-producer/hello/?name=" + name; return restTemplate.getForObject (url, String.class);}

As you may have noticed, the previous specific host:port is directly replaced by the service name eureka-producer. So why can such a request be called successfully? Because Spring Cloud Ribbon has an interceptor, it can automatically select the service instance when the actual call is made here, and replace the service name here with the actual IP address and port to be requested, thus completing the call to the service interface.

Spring Cloud Feign

In practice, we basically use Feign to complete the call. We use an example to show how easily Feign declares the definition and invocation of eureka-producer services. Welcome to join me on qq:1038774626 to discuss technical issues.

POM package configuration

Create a basic Spring Boot application named eureka-producer-feign, and add the following configuration to pom.xml:

Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-netflix-eureka-client

Startup class

Add @ EnableFeignClients to the startup class

@ EnableFeigClients@SpringBootApplicationpublic class EurekaConsumerFeignApplication {public static void main (String [] args) {SpringApplication.run (EurekaConsumerFeignApplication.class, args);}}

The method names and parameters in this class need to be the same as those in the Contoller in the remote service.

There are several pits here, which are described in detail later.

Controller

Modify Controller, inject HelloRemote into the controller layer, and call it as usual.

@ RequestMapping@Restcontroller ("/ hello") public class HelloController {@ Autowired HelloRemote helloRemote; @ GetMapping ("/ {name}") public String index (@ PathVariable ("name") String name) {return helloRemote.hello (name + "!");}}

The way to implement service invocation through Spring Cloud Feign is very simple, through the interface defined by @ FeignClient to uniformly declare the micro-service interface we need to rely on. In the specific use, it can be called at one point with the local method. Because Feign is implemented based on Ribbon, it comes with client-side load balancing and can also be extended through Ribbon's IRule. In addition, Feign also integrates Hystrix to achieve fault-tolerant protection of services.

Thank you for your reading. The above is the content of "how to use Eureka to build a simple server registration service". After the study of this article, I believe you have a deeper understanding of how to use Eureka to build a simple server registration service. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report