美文网首页
Spring Cloud Netflix 学习之二:服务注册(E

Spring Cloud Netflix 学习之二:服务注册(E

作者: Spring第二春 | 来源:发表于2017-07-07 00:52 被阅读0次

Eureka Server 建好之后(参考Spring Cloud Netflix 学习之一:服务发现模块(Eureka Server)),spring boot应用就可以注册到Eureka,提供服务。
这里以一个hello应用做例子。功能非常简单,接收个name参数,从配置文件读取端口信息,返回拼接的字符串。

@RestController
public class HelloController {
    @Value("${server.port}")
    String port;
    
    @RequestMapping(value = "/hello" ,method = RequestMethod.GET)
    public String sayHello(@RequestParam String name) {
        String r = "Hello, " + name +"! port: " + port;
        return r;
    }
}

pom.xml的denpendancies要增加eureka discovery。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

用@EnableDiscoveryClient注解开启服务注册功能

@EnableDiscoveryClient
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

application.properties 配置好Eureka Server地址

spring.application.name=app-hello
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:2000/eureka/

启动Eureka Server,再启动app-hello,访问http://localhost:2000 ,可以看到app-hello已经注册在上面。

image.png

相关文章

网友评论

      本文标题:Spring Cloud Netflix 学习之二:服务注册(E

      本文链接:https://www.haomeiwen.com/subject/tupchxtx.html