微服务教程-使用Spring Cloud

转载请注明出处 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) -
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值