Spring-cloud entry-eureka-client (Service Registration ),
Preface
In the previous section, we set up a service registration center to provide a bridge between service providers and consumers. In this section, we set up a service provider and register it with the Registration Center.
Open Source Address: https://github.com/bigbeef
Create eureka-client Module
The code structure is as follows:
Code Writing
Cppba-spring-cloud-eureka-client> pom. xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>cppba-spring-cloud-eureka-client</artifactId> <packaging>jar</packaging> <name>${project.artifactId}</name> <parent> <groupId>com.cppba</groupId> <artifactId>cppba-spring-cloud</artifactId> <version>1.0.0</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies></project>
SpringCloudEurekaClientApplication. java
package com.cppba;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublic class SpringCloudEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaClientApplication.class, args); } @Value("${server.port}") private String port; @RequestMapping("/hi") public String sayHi(@RequestParam String name) { return "hi " + name + ",i am from port:" + port; }}
Application. properties
server.port=8764eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/spring.application.name=service-say
The project has been established.
Start the project
Before starting eureka-client, we first start the eureka-server set up in the previous section. After both services are started, we access the registration center. We can see that:
This indicates that our service has been registered successfully! Visit http: // 127.0.0.1: 8764/hi? Name = hornet
At this point, the eureka-client (service discovery) project has been set up successfully.
Reference: https://github.com/bigbeef/cppba-spring-cloud
Github address: https://github.com/bigbeef
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.