服务注册中心eureka-server已经搭好,我们开始编写一个eureka-client,并提供一个hello服务
一、新建module,选择对应的springcloud模块,pom.xml如下:
<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/
手机扫一扫
移动阅读更方便
你可能感兴趣的文章