Herein, a ternary NiS@NC/CdS heterojunction is designed and synthesized by in situ depositing CdS on the hierarchical flower-like NiS2@NC derived from porous Ni-ZIF template for a superior photo/electrocatalytic hydrogen evolution. The findings reveal that the introduced Cd atoms could react with S atoms from NiS2 to form CdS, and meantime the original NiS2 crystal phase transforms into a new NiS phase, which results in tight contact between CdS and NiS, and further facilitates efficient formation of ternary heterojunction among CdS, NiS and N doped porous carbon. As a result, the synthesized ternary NiS@NC/CdS heterojunction exhibits a superior photocatalytic hydrogen generation activity, up to 15.4 mmol/gcat in 3 hours, in the absence of Pt cocatalyst, which is 2.3 and 230 times higher than that of NiS2@NC and CdS, respectively. The results demonstrate that the introduction of CdS can efficiently enhance the production of photogenerated carriers, and the presence of ternary heterojunction can further improve the separation and transportation efficiency of photogenerated carriers. Moreover, density functional theory (DFT) calculations demonstrate that the presence of CdS and carbon efficiently reduces the adsorption energy of H*, leading to a significant enhancement of hydrogen evolution performance. This work provides a new perspective for improving the photo/electrocatalytic activity of MOFs-derived carbon materials.