spring-cloud-netflix-eureka-client


服務注冊中心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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM