spring-cloud-netflix-eureka-client

Posted on 2020-10-12 13:55  FLGB  阅读(806)  评论(0编辑  收藏  举报

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

一、新建module,选择对应的springcloud模块,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>lf.liyouyou</groupId>
        <artifactId>spring-cloud-netflix-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>lf.liyouyou</groupId>
    <artifactId>spring-cloud-eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-client</name>
    <description>Demo project for Spring Boot</description>


    <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>

</project>

二、启动类添加注解


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/

Copyright © 2024 FLGB
Powered by .NET 9.0 on Kubernetes