转载请注明出处 http://www.paraller.com 原文排版地址 点击获取更好阅读体验 分割符下面的配置代表:应用程序使用cloud 环境文件运行 如果你使用了SPRING_PROFILES_ACTIVE变量,可以在 manifest.yml or, on Cloud Foundry Lattice, your Docker file.配置这个变量
在我的发布脚本中设置了APPLICATION_DOMAIN 环境变量,告诉服务外部的关联地址。 在Eureka监控界面下点击刷新,30秒后你会看到你的服务已经注册好了。
使用 Ribbon 实现客户端的负载均衡
Spring Cloud 中关联其他服务使用的是 spring.application.name . 当我们构建 Spring Cloud-based 的服务的时候,这个值可以在很多上下文中被用到
为的是让客户端基于一些上下文信息,去决定哪一个服务将会被连接使用, Spring Cloud 把client-side load-balancing用途的 Ribbon集成进来了。 看一下示例,直接使用 Eureka然后使用Ribbon.
``` package passport;
import org.apache.commons.lang.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.client.RestTemplate;
import java.util.List;
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.web(false)
.run(args);
}
}
@Component class DiscoveryClientExample implements CommandLineRunner {
@Autowired
private DiscoveryClient discoveryClient;
@Override
public void run(String... strings) throws Exception {
discoveryClient.getInstances("photo-service").forEach((ServiceInstance s) -
本文介绍如何使用 Spring Cloud 实现服务的自动注册与发现,并通过 Ribbon 实现客户端的负载均衡。具体包括配置 cloud 环境、设置 APPLICATION_DOMAIN 环境变量、在 Eureka 监控界面查看服务注册状态以及使用 Feign 和 Ribbon 进行服务调用。
1154

被折叠的 条评论
为什么被折叠?



