Spring Cloud EureKa Ribbon 服务注册发现与调用
概述 用一个简单的例子演示Spring Cloud中EureKa和Ribbon的基本用法。 版本和环境
构建eureka server 在Spring Cloud,可以使用eureka来管理微服务,微服务可以注册到eureka中。 首先可以用IDEA的 修改application.properties文件,添加如下内容 spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false server.port=8881 在Spring Boot为我们生成的启动类 package com.springcloud.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class,args); } } 在浏览器中输入http://localhost:8881/ 可以看到如下界面: 可以看到暂时还没有服务注册上来。到此,一个简单的微服务注册中心就构建完了。 编写微服务userservice 接下来用rest构建一个微服务接口,并注册到注册中心上去。依然使用 注意这次要勾选 修改application.properties文件,添加如下内容: spring.application.name=user server.port=8882 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/ 在 package com.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class,args); } } 创建一个rest full的微服务接口。 package com.springcloud; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/getUser") public String getUser() { return "I am user list."; } } 运行UserApplication后,再次访问http://localhost:8881/ 会发现user这个服务已经注册上来了。 编写微服务order 接下来,我们再构建一个订单的微服务,并访问user这个微服务中的接口。 依然使用 这次除了勾选 修改application.properties文件,添加如下内容: spring.application.name=order server.port=8883 eureka.client.service-url.defaultZone=http://localhost:8881/eureka/ 在 package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class OrderApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } } 由于使用了Ribbon,这里需要使用 编写 package com.springboot; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/getOrderUser") public String getOrderUser() { return restTemplate.getForEntity("http://user/getUser",String.class).getBody(); } } 运行 会发现order这个服务也被注册到注册中心了。 接下来我们访问 可以看到返回了:I am user list. 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |