Spring Cloudサービスのリリースと呼び出し


1.サービスの配信
  • 関連付けのpom.xmlの導入
  • 
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.1.3.RELEASE
             
        
        com.zws.cloud
        Eurka-Producer
        0.0.1-SNAPSHOT
        Eurka-Producer
        Demo project for Spring Boot
    
        
            1.8
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Greenwich.RELEASE
                    pom
                    import
                
            
        
        
    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
  • Eureka Serverアドレスaplication.yml
  • server:
      port: 8080
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    
    spring:
      application:
        name: Eureka-Producer
  • サービスをリリースするということは、Httpインターフェースです.以下の通りです.
  • package com.zws.cloud.producer;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @Author wensh.zhu
     * @Date 2019/2/16 19:33
     */
    @RestController
    public class HelloController {
    
        @GetMapping("/hello")
        public String hello() {
            return "hello";
        }
    }
    
       
    package com.zws.cloud.producer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class EurekaProducerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaProducerApplication.class, args);
        }
    
    }
    
    2.コールサービス
  • 依存配置pom.xml
  • 
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.1.3.RELEASE
             
        
        com.zws.cloud
        Eurka-Producer
        0.0.1-SNAPSHOT
        Eurka-Producer
        Demo project for Spring Boot
    
        
            1.8
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Greenwich.RELEASE
                    pom
                    import
                
            
        
        
    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-ribbon
            
    
            
            
                org.springframework.cloud
                spring-cloud-starter-openfeign
            
    
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
  • Ribbon負荷バランスとFeignインターフェース注入を有効にし、appication.yml
  • server:
      port: 8088
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    
    spring:
      application:
        name: Eureka-Consumer
  • Feignクライアントの定義と溶断フィードバック
  • package com.zws.cloud.consumer;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * @Author wensh.zhu
     * @Date 2019/2/16 15:14
     */
    @FeignClient(value = "Eureka-Producer", fallbackFactory = HelloClientFallbackFactory.class)
    public interface HelloClient {
    
        @GetMapping("/hello")
        String hello();
    }
    
    package com.zws.cloud.consumer;
    
    import feign.hystrix.FallbackFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author wensh.zhu
     * @Date 2019/2/16 16:12
     */
    @Component
    public class HelloClientFallbackFactory implements FallbackFactory, HelloClient {
        @Override
        public String create(Throwable throwable) {
            return this.hello();
        }
    
        @Override
        public String hello() {
            return "";
        }
    }
    @FeignClient(value=「Eureka-Paroducer」、fallback Factory=Hellocient FallbackFactory.class)のうち、Eurka-Paroducerはサービス提供者の実例名称(spring.appration.name)です.
  • Feignインターフェースおよび呼び出しを有効にする
  • package com.zws.cloud.EurkaProducer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    @EnableFeignClients
    @SpringBootApplication
    public class EurkaProducerApplication {
    
        @Resource
        private HelloFeign helloFeign;
    
        public static void main(String[] args) {
            SpringApplication.run(EurkaProducerApplication.class, args);
        }
    
        @GetMapping("/hello")
        public String hello() {
            return helloFeign.hello();
        }
    
    }
    3.まとめ
    サービスのリリースは主に以下の3つの方面に関連しています.
  • Eureka Serverアドレスの設定:
    ##        
    eureka:
    client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
  • Eureka Cient依存の導入
    
    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    
  • 普通のHttpインターフェースを定義する
  • サービスコール:
  • 3つの依存を導入する:
  • 
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-ribbon
    
    
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
  • Eureka Serverを配置する:
  • eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8762/eureka/
  • Feign Clientインターフェースの定義と失敗のコールバック
  • 起動類追加@EnbaleFeign Clientsコメント
  • @EnableFeignClients            Spring     ,             。