Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write Eureka services

Shulou Source: shulou.com Published: 2022-06-03 08:52:55 09月29日 Update

How to carry out Eureka writing service, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1) create a project to register with Eureka

Now that the registry is created and started, we implement to register a service provider eureka-client-user-service with Eureka and provide an interface to other service invocations.

First, create a Maven project, and then add related dependencies to pom.xml, as shown below.

Org.springframework.bootspring-boot-starter-parent2.0.6.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-eureka-clientorg.springframework.cloudspring-cloud-dependenciesFinchley.SR2pomimport

Create a startup class, App, with the following code.

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

The method of starting the class is not much different from that before, except that the annotation is replaced by @ EnableDiscoveryClient, indicating that the current service is a client of Eureka.

Next, create an application.properties properties file under src/main/resources and add the following configuration:

Spring.application.name= eureka-client-user-serviceserver.port=8081eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/# uses IP registration eureka.instance.preferIpAddress=true# to define instance ID format eureka.instance.instance-id=$ {spring.application.name}: ${spring.cloud.client.ip-address}: ${server.port}

The address of eureka.client.serviceUrl.defaultZone is the address of the Eureka service that we started earlier, and we need to register our own information with Eureka when starting.

By executing the App startup service, we can see that there is a log in the console that outputs the registration information:

DiscoveryClient_EUREKA-CLIENT-USER-SERVICE/eureka-client-user-service:192.168.31.245:8081-registration status: 204

We can further check whether the service is registered successfully. Go back to the Web console of Eureka that you opened earlier, refresh the page, and you can see the newly registered service information.

2) write and provide interface

Create a Controller that provides an interface to other service queries, as shown below.

@ RestControllerpublic class UserController {@ GetMapping ("/ user/hello") public String hello () {return "hello";}}

Restart the service and visit http://localhost:8081/user/hello (http://localhost%EF%BC%9A8081/user/hello)). If you can see the Hello string we returned, it proves that the interface has been provided successfully.

This is the answer to the question about how to write Eureka service. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Tags: Service interface code information question success next address console method more project help control solution easy easy content just character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker vpn Shulou Information Huawei