Eureka的使用

 eureka注册中心服务端: 

server:
  port: 8761
eureka:
  client:
    #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
    registerWithEureka: false
    #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

clientA 发起注册

server:
  port: 8001
#服务发现客户端
eureka:
  instance:
    #向Eureka注册时,是否使用IP地址+端口号作为服务实例的唯一标识。推荐设置为true
    prefer-ip-address: true
  client:
    #是否将自身的实例信息注册到Eureka服务端
    register-with-eureka: true
    #是否拉取并缓存其他服务注册表副本到本地
    fetch-registry: true
    # eureka client刷新本地缓存时间
    registryFetchIntervalSeconds: 5
    #注册到哪个Eureka服务实例
    service-url:
      defaultZone: http://localhost:8761/eureka/

clientB 发起注册

server:
  port: 8002
#服务发现客户端
eureka:
  instance:
    #向Eureka注册时,是否使用IP地址+端口号作为服务实例的唯一标识。推荐设置为true
    prefer-ip-address: true

  client:
    #是否将自身的实例信息注册到Eureka服务端
    register-with-eureka: true
    #是否拉取并缓存其他服务注册表副本到本地
    fetch-registry: true
    # eureka client刷新本地缓存时间
    registryFetchIntervalSeconds: 5
    #注册到哪个Eureka服务实例
    service-url:
      defaultZone: http://localhost:8761/eureka/      
      

eureka服务调用:clientA调用clientB的接口

@FeignClient("ueh-organization")
public interface OrganizationClient {
    @GetMapping("/organization/{organizationId}")
    R<OrganizationDTO> getOrganization(@PathVariable("organizationId") Long organizationId);
}    
{
    @Autowired
    private OrganizationClient organizationClient; 

    public LicenceDTO queryDetail(Long licenceId) {
        Licence licence = this.getById(licenceId);
        // 校验非空
        ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);

        OrganizationDTO org = ClientUtil.execute(() -> organizationClient.getOrganization(licence.getOrganizationId()));
        return toLicenceDTO(licence, org);
    }
}
// 远程调用工具类
public class ClientUtil {
    /**
     * 封装远程调用, 只返回关心的内容
     * @param supplier 远程调用真正逻辑, 返回内容为: {@link R<T>}
     * @param <T> 关心的内容类型
     * @return 关心的内容
     */
    public static <T> T execute(Supplier<R<T>> supplier) {
        R<T> r = supplier.get();
        CommonResponseEnum.assertSuccess(r);
        return r.getData();
    }

    /**
     * 封装远程调用, 只返回关心的内容
     * @param supplier 程调用真正逻辑, 返回内容为: {@link QR<T>}
     * @param <T> 关心的内容类型
     * @return 关心的内容
     */
    public static <T> QueryData<T> executePage(Supplier<QR<T>> supplier) {
        QR<T> qr = supplier.get();
        CommonResponseEnum.assertSuccess(qr);
        return qr.getData();
    }
}

clientB端Controller

@RequestMapping("/organization")
@RestController
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;
    @GetMapping("/{organizationId}")
    public R<OrganizationDTO> getOrganization(@PathVariable("organizationId") Long organizationId) {
        return new R<>(organizationService.queryDetail(organizationId));
    }
}

猜你喜欢

转载自www.cnblogs.com/hahajava/p/11308561.html