SpringCloud2.0 Eureka

1.搭建Eureka服务端
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

开启注解@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

配置文件

eureka:
  instance:
    prefer-ip-address: true
    ip-address: 192.168.3.5
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://192.168.3.5:8080/eureka/
  server:
      eviction-interval-timer-in-ms: 6000   # 心跳检查时间
      enableSelfPreservation: false         # 关闭保护模式
2.eureka客户端
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>  
eureka:
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 5      # 服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 10  # 服务续约到期时间(缺省为90s)
  client:
    healthcheck:
      enabled: true     # 健康检查(需要spring-boot-starter-actuator依赖)
    service-url:
      defaultZone: http://192.168.3.5:8080/eureka/

3.resttemplate调用

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();

    }
}
4.多restemplate配置
@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    RestTemplate loadBalanced() {
        return new RestTemplate();
    }

    @Primary
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

public class MyClass {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    @LoadBalanced
    private RestTemplate loadBalanced;

    public String doOtherStuff() {
        return loadBalanced.getForObject("http://stores/stores", String.class);
    }

    public String doStuff() {
        return restTemplate.getForObject("http://example.com", String.class);
    }
}

参考文档:
http://cloud.spring.io/spring-cloud-static/Camden.SR3/#_multiple_resttemplate_objects

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值