微服务教程-使用Spring Cloud

来源:互联网 发布:淘宝申请介入问题描述 编辑:程序博客网 时间:2024/05/11 00:37

转载请注明出处 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@EnableFeignClientspublic class Application {

public static void main(String[] args) {    new SpringApplicationBuilder(Application.class)            .web(false)            .run(args);}

}

@Componentclass DiscoveryClientExample implements CommandLineRunner {

@Autowiredprivate DiscoveryClient discoveryClient;@Overridepublic void run(String... strings) throws Exception {    discoveryClient.getInstances("photo-service").forEach((ServiceInstance s) -