1. 发布服务

  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
            
        
    

  1. 配置Eureka Server地址 application.yml
server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: Eureka-Producer
  1. 发布一个服务,其实就是一个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. 调用服务

  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.cloud
            spring-cloud-starter-netflix-ribbon
        

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  1. 启用Ribbon负载均衡以及Feign接口注入,application.yml
server:
  port: 8088

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: Eureka-Consumer
  1. 定义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-Producer", fallbackFactory = HelloClientFallbackFactory.class)其中Eurka-Producer为服务提供方的实例名称(spring.application.name)

  1. 启用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. 总结

服务发布主要涉及如下三个方面:

  1. 配置Eureka Server地址:
    ## 多个以逗号隔开
    eureka:
    client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
  2. 引入Eureka Client依赖
    
    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    
  3. 定义普通的Http接口

服务调用

  1. 引入三个依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client




    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon




    org.springframework.cloud
    spring-cloud-starter-openfeign
  1. 配置Eureka Server:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/
  1. 定义FeignClient接口以及失败的回调
  2. 启动类添加@EnableFeignClients注解
@EnableFeignClients会把第三步定义的接口注入Spring上下文中 ,然后使用的时候直接注入即可。