springcloudѧϰ֮·: (Ò») ×î¼òµ¥µÄ´î½¨springc¡­

2019-10-08 09:28:59À´Ô´£º²©¿ÍÔ° ÔĶÁ ()

ÐÂÀÏ¿Í»§´ó»ØÀ¡,ÔÆ·þÎñÆ÷µÍÖÁ5ÕÛ

springcloudѧϰ֮·: (Ò») ×î¼òµ¥µÄ´î½¨springcloudµÄ·½·¨

²Î¿¼×ÊÁÏ: [JavaEE] Îå·ÖÖӴSpringCloud»·¾³, ½øÈë΢·þÎñʱ´ú

¸ÐлÉÏƪ²©ÎÄ´óÀдøÁì×ß½øspringcloudÊÀ½ç, ±¾²©ÎÄÖ÷ҪĿµÄΪ¼Ç¼×Ô¼ºÑ§Ï°springcloudµÄµãµãµÎµÎ, ¸ø×Ô¼ºµÄ֪ʶ½øÐÐÕûÀí, Èç¹ûÄÜ°ïÖú¸ü¶àµÄС»ï°ïÄǾ͸üºÃÁË.

ps: ±¾ÎÄÖ÷Òª×÷Ϊ±¸Íü¼°¿ìËٴspringcloudÖ®ÓÃ, Ö»¼Ç¼Ö÷Òª²½Öè. Ô­ÎĽ²Êö¸ü¼ÓϸÖÂ, ÏëÉîÈëѧϰµÄͬѧÍƼöµã»÷ÉÏ·½Á¬½Óѧϰ.

²Ù×÷ϵͳ: Windows 10

IDE: IntelliJ IDEA 2018.3.6

JAVA: JDK 1.8.X

Meave: 3.6.0

SpringBoot: 2.1.7

1. ´´½¨Ò»¸ö¹¤³Ì

´´½¨springboot¹¤³Ì

 2. ´´½¨Eureka×¢²áÖÐÐÄ

 ÒÀȻѡÔñspringboot¹¤³Ì

 Ñ¡Ôñeureka·þÎñÆ÷

 µ¼°ü, ¿ªÆô·þÎñ¹ÜÀíÆ÷

 Îªeureka×¢²áÖÐÐÄÌí¼Ó×¢½â¿ªÆô·þÎñ

 ÅäÖÃeureka×¢²áÖÐÐÄÅäÖÃÎļþ

server:
  # ÅäÖ÷þÎñ¶Ë¿Ú
  port: 8081
eureka:
  client:
    service-url:
      # ÅäÖÃeureka·þÎñÆ÷µØÖ·
      defaultZone: http://127.0.0.1:8081/eureka
    #ÊÇ·ñÐèÒª½«×Ô¼º×¢²áµ½×¢²áÖÐÐÄ(×¢²áÖÐÐļ¯ÈºÐèÒªÉèÖÃΪtrue)
    register-with-eureka: false
    #ÊÇ·ñÐèÒªËÑË÷·þÎñÐÅÏ¢ ÒòΪ×Ô¼ºÊÇ×¢²áÖÐÐÄËùÒÔΪfalse
    fetch-registry: false

×¢ÒâËõ½ø, ÒòΪymlʹÓÃËõ½øÀ´Çø·Ö²»Í¬×ֶεÄ.

ÔËÐÐServiceEurekaApplicationÎļþÆô¶¯ÏîÄ¿, ·ÃÎÊ×¢²áÖÐÐÄ

http://localhost:8081

 3. ´´½¨Á½¸ö΢·þÎñservice-a,service-b

´´½¨Ò»¸öspringbootÄ£¿é

 Æð¸öÃû×Ö

 

 Ñ¡Ôñweb

 Ñ¡Ôñ¿Í»§Ä£¿é

 Â·ÓÉÉèÖÃ

 ÅäÖÃ΢·þÎñµÄÈë¿ÚÎļþ @EnableEurekaClient

 ÅäÖÃapplication.yml

service-a

server:
  # ·þÎñ¶Ë¿ÚºÅ
  port: 8082
spring:
  application:
    # ·þÎñÃû³Æ - ·þÎñÖ®¼äʹÓÃÃû³Æ½øÐÐͨѶ
    name: service-objcat-a
eureka:
  client:
    service-url:
      # Ìîдע²áÖÐÐÄ·þÎñÆ÷µØÖ·
      defaultZone: http://localhost:8081/eureka
    # ÊÇ·ñÐèÒª½«×Ô¼º×¢²áµ½×¢²áÖÐÐÄ
    register-with-eureka: true
    # ÊÇ·ñÐèÒªËÑË÷·þÎñÐÅÏ¢
    fetch-registry: true
  instance:
    # ʹÓÃipµØÖ·×¢²áµ½×¢²áÖÐÐÄ
    prefer-ip-address: true
    # ×¢²áÖÐÐÄÁбíÖÐÏÔʾµÄ״̬²ÎÊý
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

service-b

server:
  # ·þÎñ¶Ë¿ÚºÅ
  port: 8083
spring:
  application:
    # ·þÎñÃû³Æ - ·þÎñÖ®¼äʹÓÃÃû³Æ½øÐÐͨѶ
    name: service-objcat-b
eureka:
  client:
    service-url:
      # Ìîдע²áÖÐÐÄ·þÎñÆ÷µØÖ·
      defaultZone: http://localhost:8081/eureka
    # ÊÇ·ñÐèÒª½«×Ô¼º×¢²áµ½×¢²áÖÐÐÄ
    register-with-eureka: true
    # ÊÇ·ñÐèÒªËÑË÷·þÎñÐÅÏ¢
    fetch-registry: true
  instance:
    # ʹÓÃipµØÖ·×¢²áµ½×¢²áÖÐÐÄ
    prefer-ip-address: true
    # ×¢²áÖÐÐÄÁбíÖÐÏÔʾµÄ״̬²ÎÊý
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

ÔËÐÐ΢·þÎñ

 ·Ö±ðÔËÐÐ×¢²áÖÐÐļ°Î¢·þÎñÄ£¿é

 ³öÏֶ˿ںűíʾÆô¶¯³É¹¦

 ±àд²âÊÔ½Ó¿Ú

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestAController {

    @RequestMapping("testA")
    public String TestAController(){
        return "Hello,SpringCloud for TestA";
    }
}

ÖØÆô·þÎñ

 

 

 ·ÃÎÊÏÂÃæµØÖ·

http://localhost:8082/testA

·ÃÎʳɹ¦

 

 

ʹÓ÷þÎñbµ÷Ó÷þÎñaµÄ½Ó¿Ú
ÕâʱÎÒÃǾÍÐèÒªÓõ½eurka(×¢²áÖÐÐÄ)ºÍfeign¿Í»§¶ËÁË
Ê×ÏÈÎÒÃÇÔÚservice-bÖд´½¨interface

ÔÚ΢·þÎñbÖÐ, ´´½¨Ò»¸öServiceAFeignClient½Ó¿Ú

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

// ÌîÈë×¢²áÖÐÐÄÖеÄÓ¦ÓÃÃû, Ò²¾ÍÊÇÒªµ÷ÓõÄ΢·þÎñµÄÓ¦ÓÃÃû
// ÔÚeurekaÒ³ÃæÖпÉÒÔÕÒµ½
@FeignClient("SERVICE-OBJCAT-A")
public interface ServiceAFeignClient {
    @RequestMapping("testA")
    public String TestAController();
}

Ó¦ÓÃÃû¿ÉÒÔÔÚeurekaÖÐÕÒµ½
http://localhost:8081

ÔÚ·þÎñbÖÐÌí¼Ó¸ö¿ØÖÆÆ÷

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// Ìí¼Ó×¢½âÉùÃ÷ÊÇ×¢²áÖÐÐÄ¿Í»§¶Ë
@EnableEurekaClient
// ʵÏÖ²»Í¬×Ó·þÎñµ÷ÓÃ
@EnableFeignClients
public class TestBController {

    @Autowired
    private ServiceAFeignClient serviceAFeignClient;

    @RequestMapping("call")
    public String call(){
        String result = serviceAFeignClient.TestAController();
        return "b to a ·ÃÎʽá¹û ---" + result;
    }
}

½â¾ö@AutowiredʵÀý±¨´í

 

 

ÖØÐÂÔËÐзþÎñb ÔÚÍøÕ¾ÉÏ·ÃÎÊÊÔÊÔ°É

http://localhost:8083/call

PS: ÔÚspringcloudÖÐÒ»¸ö×Ó·þÎñµ÷ÓÃÁíÒ»¸ö×Ó·þÎñĬÈϳ¬Ê±Ê±¼äÊÇ1s, Ò²¾ÍÊÇ˵ҪÊDZ»µ÷ÓõÄ×Ó·þÎñ·µ»Ø³¬¹ýÒ»Ãë¾Í»á³öÏÖ´íÎó, Õë¶Ô´ËÎÊÌâÐèÒªÐ޸ĵ÷Ó÷þÎñµÄymlÎļþ. 

¾ÙÀý: ÔÚ±¾°¸ÀýÖÐ, service-aÊDZ»µ÷ÓÃÕß, service-bÊǵ÷ÓÃÕß, ÔòÔÚservice-bµÄymlÎļþÖмÓÈë

 

ribbon:
  #½¨Á¢Á¬½Ó³¬Ê±Ê±¼ä
  ReadTimeout: 5000
  #¶ÁÈ¡×ÊÔ´³¬Ê±¼ä
  ConnectTimeout: 5000

 

×¢ÒâÊ×ÐÐËõ½ø, service-bÍêÕûÅäÖÃÈçÏÂ

server:
  # ·þÎñ¶Ë¿ÚºÅ
  port: 8083
spring:
  application:
    # ·þÎñÃû³Æ - ·þÎñÖ®¼äʹÓÃÃû³Æ½øÐÐͨѶ
    name: service-objcat-b
eureka:
  client:
    service-url:
      # Ìîдע²áÖÐÐÄ·þÎñÆ÷µØÖ·
      defaultZone: http://localhost:8081/eureka
    # ÊÇ·ñÐèÒª½«×Ô¼º×¢²áµ½×¢²áÖÐÐÄ
    register-with-eureka: true
    # ÊÇ·ñÐèÒªËÑË÷·þÎñÐÅÏ¢
    fetch-registry: true
  instance:
    # ʹÓÃipµØÖ·×¢²áµ½×¢²áÖÐÐÄ
    prefer-ip-address: true
    # ×¢²áÖÐÐÄÁбíÖÐÏÔʾµÄ״̬²ÎÊý
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
ribbon:
  #½¨Á¢Á¬½Ó³¬Ê±Ê±¼ä
  ReadTimeout: 5000
  #¶ÁÈ¡×ÊÔ´³¬Ê±¼ä
  ConnectTimeout: 5000

 


Ô­ÎÄÁ´½Ó:https://www.cnblogs.com/zhainan-blog/p/11634621.html
ÈçÓÐÒÉÎÊÇëÓëÔ­×÷ÕßÁªÏµ

±êÇ©£º

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»­£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ£¬°æȨ¹éÔ­×÷ÕßËùÓÐ

ÉÏһƪ£º@AsyncÔ´Âë̽¾¿

ÏÂһƪ£ºº»ÊµJava»ù´¡ÏµÁÐ16£ºÒ»ÎĶÁ¶®Java IOÁ÷ºÍ³£¼ûÃæÊÔÌâ