1. 程式人生 > 實用技巧 >spring-cloud-netflix-eureka-client

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/