spring-cloud-netflix-eureka-client
阅读原文时间:2023年07月08日阅读:2

服务注册中心eureka-server已经搭好,我们开始编写一个eureka-client,并提供一个hello服务

一、新建module,选择对应的springcloud模块,pom.xml如下:


http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
lf.liyouyou spring-cloud-netflix-demo 1.0-SNAPSHOT
lf.liyouyou
spring-cloud-eureka-client
0.0.1-SNAPSHOT
spring-cloud-eureka-client
Demo project for Spring Boot

<dependencies>  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-test</artifactId>  
        <scope>test</scope>  
        <exclusions>  
            <exclusion>  
                <groupId>org.junit.vintage</groupId>  
                <artifactId>junit-vintage-engine</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  
</dependencies>

二、启动类添加注解

package lf.liyouyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudEurekaClientApplication {

public static void main(String\[\] args) {

    SpringApplication.run(SpringCloudEurekaClientApplication.class, args);  
}

}

三、编写服务代码

package lf.liyouyou.com.lf;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@RequestMapping("/hello")  
public String index(@RequestParam String name) {  
    return "hello "+name+",nice to meet you!";  
}  

}

四、application.properties

spring.application.name=spring-cloud-netflix-eureka-client-application
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

启动项目,访问http://localhost:8000/ 查看eureka面板,发现服务已经注册上去

localhost:8000/eureka/