How to realize Service Registration and Discovery in Eureka
How to achieve service registration and discovery in Eureka, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Initialize the project from Spring Initializr
Visit http://start.spring.io/ to initialize the project. We named the project micro-weather-eureka-server.
Change configuration
Follow the guidelines of the following two blogs to configure and speed up the construction of the project.
Gradle Wrapper refers to the local release package: https://waylau.com/change-gradle-wrapper-distribution-url-to-local-file/
Use Maven Mirror: https://waylau.com/use-maven-mirrors/
Enable Eureka Server
To enable EurekaServer, add the @ EnableEurekaServer annotation to Application.
@ SpringBootApplication@EnableEurekaServerpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}} modify project configuration
Modify application.properties to add the following configuration.
Server.port: 8761eureka.instance.hostname: localhosteureka.client.registerWithEureka: falseeureka.client.fetchRegistry: falseeureka.client.serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Where:
Server.port: indicates the port number on which the application is started
Eureka.instance.hostname: the host name of the application
Eureka.client.registerWithEureka: a value of false means that it is only a server, not a client
Eureka.client.fetchRegistry: a value of false means you don't need to register yourself
Eureka.client.serviceUrl.defaultZone: indicates the URL of the application
Start Eureka Server
Start the application, visit http://localhost:8761/, and you can see the UI management interface that comes with Eureka Server.
Create Eureka Client
Based on micro-weather-eureka-server, we will create a micro-weather-eureka-client as a client and demonstrate how to register itself with the registration server so that it can be invoked by other services.
Change configuration
Add the following configuration:
Dependencies {/ /... Compile ('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') / /.} the simplest Eureka Client@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic class Application {@ RequestMapping ("/ hello") public String home () {return "Hello world";} public static void main (String [] args) {SpringApplication.run (Application.class, args);}}
@ EnableDiscoveryClient enables service discovery, and as long as Eureka Client is started, it can be perceived by Eureka Server.
Project configuration:
Spring.application.name: micro-weather-eureka-clienteureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/ run
Client examples are launched on 8081 and 8082, respectively.
Java-jar micro-weather-eureka-client-1.0.0.jar-server.port=8081java-jar micro-weather-eureka-client-1.0.0.jar-server.port=8082
You can see the information about these two entities on Eureka Server.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.