1. 程式人生 > >springcloud(第七篇)springcloud ribbon with eureka

springcloud(第七篇)springcloud ribbon with eureka

spring cloud ribbon with eureka

introduction

上一篇中闡述了ribbon的基本用法,但是可以發現服務列表是通過配置得來的,實際
情況通常是由負載均衡+服務發現來實現的,通過服務發現獲取服務列表,負載均衡通過rule選擇要呼叫的服務。服務發現可以通過eureka來實現,後期會講解利用consul做服務發現。

eureka discovery service

eureka服務發現在前面的文章中已經提供到,這裡直接給出程式碼

首先是 eureka server

package com.lkl.springcloud.ribbon
; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EurekaServerApplication.class
).properties( "spring.config.name:eureka", "logging.level.com.netflix.discovery:OFF") .run(args); } }

對應配置檔案:

server.port=8761
spring.application.name= eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

在eureka上進行服務註冊SimpleApplication.java

package com.lkl.springcloud.ribbon;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@RestController
public class SimpleApplication {

    @RequestMapping("/")
    public String hello() {
        return "Hello";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(SimpleApplication.class).properties(
                "spring.config.name:simple").run(args);
    }   
}

對應client配置

server.port= 8081
spring.application.name= simple
eureka.instance.leaseRenewalIntervalInSeconds= 10
eureka.client.registryFetchIntervalSeconds= 5
eureka.client.serviceUrl.defaultZone= http://localhost:${eureka.port:8761}/eureka/

ribbon + eureka

Application.java

package com.lkl.springcloud.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
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.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by liaokailin on 16/5/9.
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Application {

    @Autowired
    RestTemplate client;

    /**
     * LoadBalanced 註解表明restTemplate使用LoadBalancerClient執行請求
     *
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        RestTemplate template = new RestTemplate();
        SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) template.getRequestFactory();
        factory.setConnectTimeout(3000);
        factory.setReadTimeout(3000);
        return template;
    }

    @RequestMapping("/")
    public String helloWorld() {
        return client.getForObject("http://simple/", String.class);
    }


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


}

EnableDiscoveryClient 表明為eureka client 通過可以進行服務發現

RestTemplate 構建RestTemplate對應的bean,在method上使用註解LoadBalanced表示restTemplate使用LoadBalancerClient執行請求

對應配置檔案

spring.application.name=RibbionClient
eureka.instance.leaseRenewalIntervalInSeconds= 10
eureka.client.registryFetchIntervalSeconds= 5
eureka.client.serviceUrl.defaultZone= http://localhost:${eureka.port:8761}/eureka/

run Application.java後訪問http://localhost:8080/呼叫helloWorld()方法,該方法訪問http://simple/ 其中的simpleSimpleApplication.java
在eureka上註冊的service id,即實際呼叫

@RequestMapping("/")
    public String hello() {
        return "Hello";
    }

因此得到返回結果Hello.

ok ~ it’s work ! more about is here

歡迎關注,您的肯定是對我最大的支援

這裡寫圖片描述